Site: LeetCode
Difficulty per Site: Medium
Write a solution to report the Capital gain/loss for each stock.
The Capital gain/loss of a stock is the total gain or loss after buying and selling the stock one or many times.
Return the result table in any order. [Full Description]
-- Submitted Solution
WITH cte AS (
SELECT
stock_name
,CASE WHEN operation = 'Buy' THEN -price ELSE price END AS new_price
FROM Stocks
)
SELECT
stock_name
,SUM(new_price) AS capital_gain_loss
FROM cte
GROUP BY stock_name
;-- LeetCode Solution
SELECT
stock_name,
SUM(
CASE
WHEN operation = 'buy' THEN -price
WHEN operation = 'sell' THEN price
END
) AS capital_gain_loss
FROM Stocks
GROUP BY stock_nameTBD
TBD