🤔 문제 분석
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
'코딩테스트 > 리트코드 SQL' 카테고리의 다른 글
[리트코드/SQL] 30. Duplicate Emalis (182) (0) | 2022.12.23 |
---|---|
[리트코드/SQL] 28. Top Travellers (1407) (0) | 2022.12.23 |
[리트코드/SQL] 26. Find Total Time Spent by Each Employee (1741) (0) | 2022.12.21 |
[리트코드/SQL] 25. The Latest Login in 2020 (1890) (0) | 2022.12.18 |
[리트코드/SQL] 24. Game Play Analysis I (511) (0) | 2022.12.18 |
댓글