计算同一列中两个值的差异
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了计算同一列中两个值的差异相关的知识,希望对你有一定的参考价值。
当我将记录与相同的CATEGORY
值组合时,我想找到差异(利润和损失)(例如,A将被组合并且C将被组合)。
答案
我想你想要条件聚合:
select category,
sum(iif(side = "BUY", - quantity * price, quantity * price)) as net
from t
where side in ("BUY", "SELL") -- may not be necessary
group by category;
另一答案
Select category, buy.amt-sell.amt ProfitorLoss
from
(SELECT sum(price*quantity) amt, Category
FROM yourtable
WHERE side = 'BUY'
GROUP BY Category) buy,
(SELECT sum(price*quantity) amt, Category
FROM yourtable
WHERE side = 'SELL'
GROUP BY Category) sell
where buy.category = sell.category
以上是关于计算同一列中两个值的差异的主要内容,如果未能解决你的问题,请参考以下文章