2022/12/20

LeetCode 1393. Capital Gain/Loss

問題:計算買賣損失
解法:透過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;