求SQL语句,谢谢
求一SQL语句
t1--已经申报出口的产品及数量
产品编号, 产品版本,申报出口数量
PdtNumber,Edition,Out,
k1 v1 2000
k1 v2 2500
k2 v1 3000
k1 v3 5000
k1 v1 1000
t2--已经出口的产品及数量
产品编号, 产品版本,已经出口数量
PdtNumber,Edition,OutEd
k1 v2 800
k2 v1 200
PdtNumber和Edition组成一个产品,在t1,t2中可以重复
求一SQL语句,返回这样的结果
:
PdtNumber,Edition,申报出口总数,已经申报出口总数,剩下可申报出口总数
------解决方案--------------------没测试
select t1.PdtNumber,
t1.Edition,
isnull(sum([t1.Out]),0) as '申报出口总数 ',
isnull(sum(t2.OutEd),0) as '已经申报出口总数 ',
isnull(sum([t1.Out]),0)-isnull(sum(t2.OutEd),0) as '剩下可申报出口总数 '
from t1
left join (select PdtNumber,Edition,sum(OutEd) as OutEd from t2 group by PdtNumber,Edition)t2 on t1.PdtNumber=t2.PdtNumber and t1.Edition=t2.Edition
group by t1.PdtNumber,t1.Edition