读书人

请大家帮忙看看这条SQL该如何写

发布时间: 2012-10-11 10:16:10 作者: rapoo

请大家帮忙看看这条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

读书人网 >Mysql

热点推荐