求SQL语句,统计商品的净卖出量。谢谢!
现有商品p1,p2,有一表记录一个月来的买入和卖出的数量。
Product BS Quantity
P1 B 100
P1 B 200
P1 S 500
p2 B 400
p2 S 900
P2 S 100
现在想统计每个产品这个月来的净卖出量。希望得到这样的结果:
P1 200 //500-200-100
p2 600 //900+100-400
多谢各位~
[解决办法]
select Product,sum(iif(BS="B",-Quantity,Quantity)) as Quantity from cTableName group by Product
[解决办法]
select Product, sum(iif(BS='S',1,-1)*Quantity) as qty from 销售记录表 group by Product