select 日期,源单单号,CompanyTax,仓位,物料名称,发料仓库,领料部门,成本对象
,sum(申请数量) as 申请数量,sum(出库数量) as 出库数量
into #a from k3_领料单
--where 日期>=@startDate and 日期<= @endDate
group by 日期,仓位,物料名称,发料仓库,源单单号,CompanyTax,领料部门,成本对象
select CompanyTax,日期,物料名称,源单单号,sum(申请数量) as 总申请数量,sum(出库数量) as 总出库数量
into #b from k3_领料单
--where 日期>=@startDate and 日期<= @endDate
group by CompanyTax,日期,物料名称,源单单号
select a.物料名称,a.仓位,a.CompanyTax,a.源单单号,sum(申请数量) as 申请数量,sum(出库数量) as 出库数量
,sum(申请数量)/sum(总申请数量) as 申请百分比,sum(出库数量)/sum(总出库数量) as 出库百分比
into #xa from #a a
left join
#b b
on a.源单单号 = b.源单单号 and a.CompanyTax=b.CompanyTax
and a.物料名称 = b.物料名称 and a.日期 = b.日期
where a.CompanyTax in('91450300198864428K')
and 领料部门='大米车间' and 发料仓库='原料仓' and a.源单单号<>'' and 成本对象 like '%半成品%'
group by a.CompanyTax,a.源单单号,a.物料名称,a.仓位
select CompanyTax,批号,产品名称,任务单编号
into #xq from k3_生产任务单
select * from #xa xa
left join
#xq xq on xa.源单单号=xq.任务单编号 and xa.CompanyTax=xq.CompanyTax
select CompanyTax,生产批次,源单单号,产品入库仓位,日期,年月,产品名称,case when 入库名称='四号白米筛碎米' then '4号筛碎米' else 入库名称 end as 入库名称,sum(入库数量)as 副产品入库数量
into #rf from #c
group by CompanyTax,生产批次,源单单号,产品入库仓位,日期,年月,产品名称,case when 入库名称='四号白米筛碎米' then '4号筛碎米' else 入库名称 end
select * from #xa xa
select * from #xq
select * from #rf
rfxp
select rf.CompanyTax,rf.生产批次,rf.源单单号,rf.产品入库仓位,rf.日期,rf.年月,rf.产品名称,rf.入库名称,rf.副产品入库数量*xa.出库百分比 as 副产品入库数量,xp.销售单价,(rf.副产品入库数量*xp.销售单价*xa.出库百分比)as 副产品金额
,xa.仓位
,xa.申请百分比,xa.出库百分比
into #rfxp from #xa xa
left join #xq xq
on xa.源单单号=xq.任务单编号 and xa.CompanyTax=xq.CompanyTax
left join #rf rf
on xq.批号 = rf.生产批次 and xa.CompanyTax = rf.CompanyTax
and xa.物料名称 like concat(rf.产品名称,'%')
left join #xp xp
on rf.入库名称=xp.副产品名称
order by 日期 desc
select CompanyTax,日期,产品名称,产品入库仓位,仓位,源单单号,生产批次,入库名称 from #rfxp rfxp
where 入库名称 like '%3号%'
group by CompanyTax,日期,产品名称,产品入库仓位,仓位,源单单号,生产批次,入库名称
order by 日期 desc
select CompanyTax,生产批次,源单单号,产品入库仓位 as 副产品仓位,日期,产品名称,仓位,sum(副产品金额)as 副产品金额
,sum(case when 入库名称='谷壳' then 副产品金额 end)as 谷壳金额
,sum(case when 入库名称='细糠' then 副产品金额 end)as 细糠金额
,sum(case when 入库名称='4号筛碎米' then 副产品金额 end)as '4号筛碎米金额'
,sum(case when 入库名称='1号筛乳料碎米' then 副产品金额 end)as '1号筛乳料碎米金额'
,sum(case when 入库名称='3号筛粉碎米' then 副产品金额 end)as '3号筛粉碎米金额'
,sum(case when 入库名称='3号筛自用碎米' then 副产品金额 end)as '3号筛自用碎米金额'
,sum(case when 入库名称='1号色选机杂料米' then 副产品金额 end)as '1号色选机杂料米金额'
,sum(case when 入库名称='谷壳' then 副产品入库数量 end)as 谷壳数量
,sum(case when 入库名称='细糠' then 副产品入库数量 end)as 细糠数量
,sum(case when 入库名称='4号筛碎米' then 副产品入库数量 end)as '4号筛碎米数量'
,sum(case when 入库名称='1号筛乳料碎米' then 副产品入库数量 end)as '1号筛乳料碎米数量'
,sum(case when 入库名称='3号筛粉碎米' then 副产品入库数量 end)as '3号筛粉碎米数量'
,sum(case when 入库名称='3号筛自用碎米' then 副产品入库数量 end)as '3号筛自用碎米数量'
,sum(case when 入库名称='1号色选机杂料米' then 副产品入库数量 end)as '1号色选机杂料米数量'
,sum(case when 入库名称='谷壳' then 销售单价 end)as 谷壳单价
,sum(case when 入库名称='细糠' then 销售单价 end)as 细糠单价
,sum(case when 入库名称='4号筛碎米' then 销售单价 end)as '4号筛碎米单价'
,sum(case when 入库名称='1号筛乳料碎米' then 销售单价 end)as '1号筛乳料碎米单价'
,sum(case when 入库名称='3号筛粉碎米' then 销售单价 end)as '3号筛粉碎米单价'
,sum(case when 入库名称='3号筛自用碎米' then 销售单价 end)as '3号筛自用碎米单价'
,sum(case when 入库名称='1号色选机杂料米' then 销售单价 end)as '1号色选机杂料米单价'
,avg(申请百分比) as 申请百分比,avg(出库百分比) as 出库百分比
from #rfxp rfxp
group by CompanyTax,日期,产品名称,产品入库仓位,仓位,源单单号,生产批次
order by 日期 desc
select * from #rfxp order by 日期 desc
SELECT year(日期)*100+month(日期)as 年月,副产品名称,avg(副产品售价) as 销售单价
into #xp FROM 大米生产_副产品维护表
group by 副产品名称,year(日期)*100+month(日期)
select * from 大米生产_副产品维护表