计算同一列中两个值的差异

时间:2018-03-02 21:43:00

标签: sql ms-access

当我将具有相同CATEGORY值的记录组合在一起时,我想找到差异(利润和损失)(例如,将组合A'并且将组合C')。

2 个答案:

答案 0 :(得分:2)

我认为你想要条件聚合:

A["b"]

答案 1 :(得分:1)

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