解法:透過SUM搭配CASE WHEN即可
SELECT stock_name,
SUM(
CASE
WHEN operation = 'Buy' THEN -1 * price
WHEN operation = 'sell' THEN price
END
) AS capital_gain_loss
FROM Stocks
GROUP BY stock_name;