|
说明:1.计划表 →jihua ; 生产统计表→shengchan
2.没有发现你的原始表中有产品043G510C-C 的订单数量,所以统计结果为0
-------------------------------------------------------------------------------------------------------------------
select a.订单号,a.产品名称,a.订单数量,a.计划数量,isnull(b.合格品数,0)as 合格品数 from
(select jh.订单号,jh.产品名称,SUM(jh.订单数量)as 订单数量,SUM(jh.计划数量) as 计划数量
from ceshi..jihua jh
where CONVERT(varchar(20),jh.生产日期,23)='2013-03-14'
group by jh.订单号,jh.产品名称) a
left join
(select sc.订单号,sc.产品名称,SUM(sc.合格品数) as 合格品数
from ceshi..shengchan sc
where CONVERT(varchar(20),sc.生产日期,23)='2013-03-14'
group by sc.订单号,sc.产品名称) b on a.订单号=b.订单号
union
select b.订单号,b.产品名称,isnull(a.订单数量,0)as 订单数量,isnull(a.计划数量,0) as 计划数量,b.合格品数 from
(select sc.订单号,sc.产品名称,SUM(sc.合格品数) as 合格品数
from ceshi..shengchan sc
where CONVERT(varchar(20),sc.生产日期,23)='2013-03-14'
group by sc.订单号,sc.产品名称) b
left join
(select jh.订单号,jh.产品名称,SUM(jh.订单数量)as 订单数量,SUM(jh.计划数量) as 计划数量
from ceshi..jihua jh
where CONVERT(varchar(20),jh.生产日期,23)='2013-03-14'
group by jh.订单号,jh.产品名称) a on a.订单号=b.订单号 |
-
|