Snoopy [리트코드/SQL] 27. Capital Gain/Loss(1393)
코딩테스트/리트코드 SQL

[리트코드/SQL] 27. Capital Gain/Loss(1393)

Sooyoon Jeong 2022. 12. 21.

🤔 문제 분석

1) Write an SQL query to report the Capital gain/loss for each stock.

2) The Capital gain/loss of a stock is the total gain or loss after buying and selling the stock one or many times.

 

💡 풀이

SELECT stock_name, SUM(IF(operation = "Buy", -price, price)) AS capital_gain_loss
FROM Stocks
GROUP BY stock_name

댓글