日期:2014-05-18 浏览次数:20566 次
select 产品名称代码,数量-isnull((select sum(数量) from tb2 where 产品名称代码=a.产品名称代码),0) -isnull((select sum(数量) from tb3 where 产品名称代码=a.产品名称代码),0) from tb1 a
------解决方案--------------------
select tb1.产品名称代码,sum(isnull(tb1.数量,0)) -sum(isnull(tb2.数量,0))-sum(isnull(tb3.数量,0)) -sum(isnull(tb4.数量,0)) from tb1 left join tb2 on tb1.产品名称代码 =tb2.产品名称代码 left join tb3 on tb1.产品名称代码 =tb3.产品名称代码 left join tb4 on tb1.产品名称代码 =tb4.产品名称代码
------解决方案--------------------
select a.产品名称代码, a.数量-isnull(sum(b.数量),0) from tb1 a, (select * from tb2 union select * from tb3)b where a.产品名称代码=b.产品名称代码 group by a.产品名称代码,a.数量
------解决方案--------------------
--上边全写成tb1了 select 产品名称代码,数量=sum(数量*mark) from (select *,1 as mark from tb1 union all select *,-1 as mark from tb2 union all select *,-1 as mark from tb3) t group by 产品名称代码
------解决方案--------------------
select a.产品名称代码,a.数量-isnull(sum(b.数量),0) from tb1 a,(select * from tb2 union select * from tb3)b where a.产品名称代码=b.产品名称代码 group by a.产品名称代码,a.数量