SQL构建成本计算单
select * from
(SELECT T2.FENTRYID, T3.FYEAR as 年度, T3.FPERIOD as 期间, T0.FORDERBILLNO as 销售订单,
CASE WHEN T0.FOrderBillSeq = 0 THEN ' ' ELSE CONVERT(VARCHAR(8000), T0.FOrderBillSeq) END as 销售订单分录号,
T1.FPROORDERTYPE as 类型, T1.FBILLENTRYID as 单据内码, isnull(T4.FNAME,'') as 成本中心, t5.FNUMBER as 物料编码, t1.FPRODUCTNO as 工单编号,
CASE WHEN T1.FBILLSeq = 0 THEN ' ' ELSE CONVERT(VARCHAR(8000), T1.FBILLSeq) END as 工单分录号,
T6.FNUMBER as BOM版本, isnull(T7.FNUMBER,'') as 批号, T0.FPLANQTY as 计划数量, T2.FBEGINQTY as 期初数量, T2.FBEGINAMOUNT as 期初成本,
T2.FADJCURRINPUTQTY as 期初调整数量, T2.FADJCURRINPUTAMOUNT as 期初调整金额, T2.FCURRINPUTQTY as 本期投入数量, T2.FCURRINPUTAMOUNT as 本期投入金额,
T2.FSUMCURRINPUTQTY as 累计投入数量, T2.FSUMCURRINPUTAMOUNT as 累计投入金额, T2.FENDQTY as 期末在产数量, T2.FENDAMOUNT as 期末在产金额,
(T2.FQUALIFIEDINQTY + T2.FDYSINQTY + T2.FWASTEINQTY) 本期完工数量,
(T2.FQUALIFIEDINAMOUNT + T2.FDYSINAMOUNT + T2.FWASTEINAMOUNT) 本期完工金额,
(T2.FSUMQUALIFIEDINQTY + T2.FSUMDYSINQTY + T2.FSUMWASTEINQTY) as 累计完工数量,
(T2.FSUMQUALIFIEDINAMOUNT + T2.FSUMDYSINAMOUNT + T2.FSUMWASTEINAMOUNT) as 累计完工成本,
ISNULL(btype_l.FNAME, N' ') as 业务类型
FROM T_CB_PROORDERINFO_H T0
INNER JOIN T_CB_PROORDERDIME T1 ON T0.FPRODUCTDIMEID = T1.FPRODUCTDIMEID
INNER JOIN T_BD_MATERIAL product_M ON product_M.FMATERIALID = T1.FPRODUCTID
INNER JOIN T_CB_CostCalExpense_H T2 ON T2.FID = T0.FID
INNER JOIN T_HS_OUTACCTG T3 ON T0.FACCTGID = T3.FID
LEFT OUTER JOIN T_CB_COSTCENTER center ON T1.FCOSTCENTERID = center.FCOSTCENTERID
LEFT OUTER JOIN T_BAS_BILLTYPE_L btype_l ON (btype_l.FBILLTYPEID = T0.FBILLTYPEID AND btype_l.FLOCALEID = 2052)
left join T_CB_COSTCENTER_L t4 on t1.FCOSTCENTERID=t4.FCOSTCENTERID and t4.FLOCALEID=2052
inner join T_BD_MATERIAL t5 on t1.FPRODUCTID=t5.FMATERIALID
inner join T_BD_MATERIAL_L t05 on t1.FPRODUCTID=t05.FMATERIALID and t05.FLOCALEID=2052
left join T_ENG_BOM t6 on t0.FBomID=t6.FID
left join T_BD_LOTMASTER t7 on t0.FLOT=t7.FLOTID
WHERE T0.FENDINITKEY = '1' AND T3.FDIMENSIONID = 1 AND (T3.FYEAR * 100) + T3.FPERIOD >= 202310
AND (T3.FYEAR * 100) + T3.FPERIOD <= 202310) A ----构建汇总数据
left join
(
SELECT FENTRYID,
sum(case when TL.FNAME='直接材料' then t.FBeginAmount else 0 end) as 期初在产品直接材料,
sum(case when TL.FNAME='间接材料' then t.FBeginAmount else 0 end) as 期初在产品间接材料,
sum(case when TL.FNAME='制造费用_其他' then t.FBeginAmount else 0 end) as 期初在产品制造费用_其他,
sum(case when TL.FNAME='五险' then t.FBeginAmount else 0 end) as 期初在产品五险,
sum(case when TL.FNAME='公积金' then t.FBeginAmount else 0 end) as 期初在产品公积金,
sum(case when TL.FNAME='委外加工费' then t.FBeginAmount else 0 end) as 期初在产品委外加工费,
sum(case when TL.FNAME='直接人工' then t.FBeginAmount else 0 end) as 期初在产品直接人工,
sum(case when TL.FNAME='折旧费用' then t.FBeginAmount else 0 end) as 期初在产品折旧费用,
sum(case when TL.FNAME='水电费' then t.FBeginAmount else 0 end) as 期初在产品水电费,
sum(case when TL.FNAME='直接材料' then t.FADJCURRINPUTAMOUNT else 0 end) as 期初调整直接材料,
sum(case when TL.FNAME='间接材料' then t.FADJCURRINPUTAMOUNT else 0 end) as 期初调整间接材料,
sum(case when TL.FNAME='制造费用_其他' then t.FADJCURRINPUTAMOUNT else 0 end) as 期初调整制造费用_其他,
sum(case when TL.FNAME='五险' then t.FADJCURRINPUTAMOUNT else 0 end) as 期初调整五险,
sum(case when TL.FNAME='公积金' then t.FADJCURRINPUTAMOUNT else 0 end) as 期初调整公积金,
sum(case when TL.FNAME='委外加工费' then t.FADJCURRINPUTAMOUNT else 0 end) as 期初调整委外加工费,
sum(case when TL.FNAME='直接人工' then t.FADJCURRINPUTAMOUNT else 0 end) as 期初调整直接人工,
sum(case when TL.FNAME='折旧费用' then t.FADJCURRINPUTAMOUNT else 0 end) as 期初调整折旧费用,
sum(case when TL.FNAME='水电费' then t.FADJCURRINPUTAMOUNT else 0 end) as 期初调整水电费,
sum(case when TL.FNAME='直接材料' then t.FCURRINPUTAMOUNT else 0 end) as 本期投入直接材料,
sum(case when TL.FNAME='间接材料' then t.FCURRINPUTAMOUNT else 0 end) as 本期投入间接材料,
sum(case when TL.FNAME='制造费用_其他' then t.FCURRINPUTAMOUNT else 0 end) as 本期投入制造费用_其他,
sum(case when TL.FNAME='五险' then t.FCURRINPUTAMOUNT else 0 end) as 本期投入五险,
sum(case when TL.FNAME='公积金' then t.FCURRINPUTAMOUNT else 0 end) as 本期投入公积金,
sum(case when TL.FNAME='委外加工费' then t.FCURRINPUTAMOUNT else 0 end) as 本期投入委外加工费,
sum(case when TL.FNAME='直接人工' then t.FCURRINPUTAMOUNT else 0 end) as 本期投入直接人工,
sum(case when TL.FNAME='折旧费用' then t.FCURRINPUTAMOUNT else 0 end) as 本期投入折旧费用,
sum(case when TL.FNAME='水电费' then t.FCURRINPUTAMOUNT else 0 end) as 本期投入水电费,
sum(case when TL.FNAME='直接材料' then t.FSUMCURRINPUTAMOUNT else 0 end) as 累计投入直接材料,
sum(case when TL.FNAME='间接材料' then t.FSUMCURRINPUTAMOUNT else 0 end) as 累计投入间接材料,
sum(case when TL.FNAME='制造费用_其他' then t.FSUMCURRINPUTAMOUNT else 0 end) as 累计投入制造费用_其他,
sum(case when TL.FNAME='五险' then t.FSUMCURRINPUTAMOUNT else 0 end) as 累计投入五险,
sum(case when TL.FNAME='公积金' then t.FSUMCURRINPUTAMOUNT else 0 end) as 累计投入公积金,
sum(case when TL.FNAME='委外加工费' then t.FSUMCURRINPUTAMOUNT else 0 end) as 累计投入委外加工费,
sum(case when TL.FNAME='直接人工' then t.FSUMCURRINPUTAMOUNT else 0 end) as 累计投入直接人工,
sum(case when TL.FNAME='折旧费用' then t.FSUMCURRINPUTAMOUNT else 0 end) as 累计投入折旧费用,
sum(case when TL.FNAME='水电费' then t.FSUMCURRINPUTAMOUNT else 0 end) as 累计投入水电费,
sum(case when TL.FNAME='直接材料' then t.FENDAMOUNT else 0 end) as 期末在产直接材料,
sum(case when TL.FNAME='间接材料' then t.FENDAMOUNT else 0 end) as 期末在产间接材料,
sum(case when TL.FNAME='制造费用_其他' then t.FENDAMOUNT else 0 end) as 期末在产制造费用_其他,
sum(case when TL.FNAME='五险' then t.FENDAMOUNT else 0 end) as 期末在产五险,
sum(case when TL.FNAME='公积金' then t.FENDAMOUNT else 0 end) as 期末在产公积金,
sum(case when TL.FNAME='委外加工费' then t.FENDAMOUNT else 0 end) as 期末在产委外加工费,
sum(case when TL.FNAME='直接人工' then t.FENDAMOUNT else 0 end) as 期末在产直接人工,
sum(case when TL.FNAME='折旧费用' then t.FENDAMOUNT else 0 end) as 期末在产折旧费用,
sum(case when TL.FNAME='水电费' then t.FENDAMOUNT else 0 end) as 期末在产水电费,
sum(case when TL.FNAME='直接材料' then (FQUALIFIEDINAMOUNT + FDYSINAMOUNT + FWASTEINAMOUNT) else 0 end) as 本期完工直接材料,
sum(case when TL.FNAME='间接材料' then (FQUALIFIEDINAMOUNT + FDYSINAMOUNT + FWASTEINAMOUNT) else 0 end) as 本期完工间接材料,
sum(case when TL.FNAME='制造费用_其他' then (FQUALIFIEDINAMOUNT + FDYSINAMOUNT + FWASTEINAMOUNT) else 0 end) as 本期完工制造费用_其他,
sum(case when TL.FNAME='五险' then (FQUALIFIEDINAMOUNT + FDYSINAMOUNT + FWASTEINAMOUNT) else 0 end) as 本期完工五险,
sum(case when TL.FNAME='公积金' then (FQUALIFIEDINAMOUNT + FDYSINAMOUNT + FWASTEINAMOUNT) else 0 end) as 本期完工公积金,
sum(case when TL.FNAME='委外加工费' then (FQUALIFIEDINAMOUNT + FDYSINAMOUNT + FWASTEINAMOUNT) else 0 end) as 本期完工委外加工费,
sum(case when TL.FNAME='直接人工' then (FQUALIFIEDINAMOUNT + FDYSINAMOUNT + FWASTEINAMOUNT) else 0 end) as 本期完工直接人工,
sum(case when TL.FNAME='折旧费用' then (FQUALIFIEDINAMOUNT + FDYSINAMOUNT + FWASTEINAMOUNT) else 0 end) as 本期完工折旧费用,
sum(case when TL.FNAME='水电费' then (FQUALIFIEDINAMOUNT + FDYSINAMOUNT + FWASTEINAMOUNT) else 0 end) as 本期完工水电费,
sum(case when TL.FNAME='直接材料' then (FSUMQUALIFIEDINAMOUNT + FSUMDYSINAMOUNT + FSUMWASTEINAMOUNT) else 0 end) as 累计完工直接材料,
sum(case when TL.FNAME='间接材料' then (FSUMQUALIFIEDINAMOUNT + FSUMDYSINAMOUNT + FSUMWASTEINAMOUNT) else 0 end) as 累计完工间接材料,
sum(case when TL.FNAME='制造费用_其他' then (FSUMQUALIFIEDINAMOUNT + FSUMDYSINAMOUNT + FSUMWASTEINAMOUNT) else 0 end) as 累计完工制造费用_其他,
sum(case when TL.FNAME='五险' then (FSUMQUALIFIEDINAMOUNT + FSUMDYSINAMOUNT + FSUMWASTEINAMOUNT) else 0 end) as 累计完工五险,
sum(case when TL.FNAME='公积金' then (FSUMQUALIFIEDINAMOUNT + FSUMDYSINAMOUNT + FSUMWASTEINAMOUNT) else 0 end) as 累计完工公积金,
sum(case when TL.FNAME='委外加工费' then (FSUMQUALIFIEDINAMOUNT + FSUMDYSINAMOUNT + FSUMWASTEINAMOUNT) else 0 end) as 累计完工委外加工费,
sum(case when TL.FNAME='直接人工' then (FSUMQUALIFIEDINAMOUNT + FSUMDYSINAMOUNT + FSUMWASTEINAMOUNT) else 0 end) as 累计完工直接人工,
sum(case when TL.FNAME='折旧费用' then (FSUMQUALIFIEDINAMOUNT + FSUMDYSINAMOUNT + FSUMWASTEINAMOUNT) else 0 end) as 累计完工折旧费用,
sum(case when TL.FNAME='水电费' then (FSUMQUALIFIEDINAMOUNT + FSUMDYSINAMOUNT + FSUMWASTEINAMOUNT) else 0 end) as 累计完工水电费
FROM
T_CB_COSTCALEXPENSEDETAIL_H T
INNER JOIN
T_HS_COSTITEM_L TL ON T.FCOSTITEMID=TL.FCOSTITEMID
AND FLOCALEID =2052
group by FENTRYID
) B ON A.FENTRYID=B.FENTRYID---根据成本项目构建对应的明细信息
作者:weiweibrother
来源:金蝶云社区
原文链接:https://wenku.my7c.com/article/510417302119237376?productLineId=1
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
SQL构建成本计算单
本文2024-09-16 17:44:45发表“云星空知识”栏目。
本文链接:https://wenku.my7c.com/article/kingdee-k3cloud-17673.html