111,120
社区成员
发帖
与我相关
我的任务
分享
请看看结果,不对呀。
select a.product,a.aqty,sum(b.bqty) bqty from plana a join planb b on a.product=b.product group by a.product,a.aqty order by a.product
例如下图的结果要加入对B表的数量汇总。
Declare @tableA table(product varchar(10) ,aqty int ,madate datetime)
Declare @tableB table(product varchar(10) ,bqty int ,madate datetime)
insert @tableA
select 'pa',100,'2013-8-1' union all
select 'pb',28 ,'2013-8-2' union all
select 'pc',50 ,'2013-8-1'
insert @tableB
select 'pa', 50, '2013-8-1' union all
select 'pa', 30, '2013-8-2' union all
select 'pb', 28, '2013-8-2' union all
select 'pc', 50, '2013-8-1' union all
select 'pd', 90, '2013-8-2'
--select * from @tableA
--select * from @tableB
select distinct t.product
,Aqty=(select Sum(a.aqty) from @tableA a where a.product=t.product )
,Bqty=(select Sum(b.bqty) from @tableB b where b.product=t.product )
from
(
select product from @tableA
union
select product from @tableB
) t
/*
product Aqty Bqty
pa 100 80
pb 28 28
pc 50 50
pd NULL 90
*/
看看有木有更简单的写法吧