LeetCode - 1393. Capital Gain/Loss (MySQL)

조민수·2024년 6월 16일
0

LeetCode

목록 보기
38/61

Medium, SQL - IF

RunTime : 469 ms


문제

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.

The result format is in the following example.


풀이

  • 처음에는 Sell인 값들의 합 - Buy인 값들의 합으로 하려했는데
    단순히 IF을 잘 사용하면 차 계산을 쉽게 할 수 있었다.
SELECT stock_name,
SUM(IF(operation = 'Sell', price, -price)) AS capital_gain_loss
FROM Stocks
GROUP BY 1
profile
사람을 좋아하는 Front-End 개발자

0개의 댓글