日期:2014-05-16  浏览次数:20825 次

请大家帮忙看看这条SQL该怎么写
有这样一个产品进出货表,product是产品名,qty为数量,type表示进或出货:
product qty type
-------------------------
phone 20 in
computer 22 in
phone 15 in
dc 15 in
computer 20 out
dc 30 in
phone 10 out
-------------------------

要求编写SQL语句输出如下结构:

产品 进货数 库存
-------------------------
phone 35 25
computer 22 2
dc 45 45
-------------------------

请教各位大神该怎么写这条SQL?谢谢!

------解决方案--------------------
select product,sum(iif(type='in',qty,0)) as 进货数,sum(iif(type='in',qty,-1*qty)) as 库存
from tt group by product
------解决方案--------------------
SELECT product,SUM(IIF(type='in',qty,0)) as 进货数,
SUM(IIF(type='in',qtp,-1*qty))as库存
FROM tb
GROUP BY product;
------解决方案--------------------
探讨

select product,sum(iif(type='in',qty,0)) as 进货数,sum(iif(type='in',qty,-1*qty)) as 库存
from tt group by product