请大家帮忙看看这条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 -------------------------
------解决方案--------------------
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;
------解决方案--------------------