问题描述
我想求出这样的结果,同一张单据(id相同为同一张)利润unitCost*inQuantity-unitCost*outQuantity最后的结果期望是这样的sn id pricesDB-20111129-61644 1042 50DB-20111129-81334 1043 -400原记录与附件中的图片一样谢谢! 问题补充:huoyj 写道
解决方案
select sn,id, sum(unitCost*inQuantity-unitCost*outQuantity) prices from t_v group by rollup(sn,id);select sn,id, sum(unitCost*inQuantity-unitCost*outQuantity) prices 这里就是查询sn,id 计算利润,as prices是给利润列起个别名叫prices,sum计算的时候是计算一个组里的所有的和,也就是group by分到一个组里的。group by rollup(sn,id); --由sn和id联合分组,即如果两条记录的sn和id相同,那就分到同一个组里面。
解决方案二:
select sn,id,sum(unitCost*inQuantity-unitCost*outQuantity) prices --prices应该是利润吧from XXXXX --不知道你的表名group by rollup(sn,id);写了一个,你看看符合要求嘛
时间: 2024-08-02 04:28:56