๋ฌธ์
https://leetcode.com/problems/capital-gainloss/
์ฝ๋
select stock_name, sum(prices) as capital_gain_loss
from (select stock_name,
case
when operation = 'Buy' then -price -- price * -1 ๋ก๋ ๊ฐ๋ฅ
else price
end as prices
from stocks) as t
group by 1
-- 10๋ถ
๋ฌธ์ ํ์ด
case๋ฌธ์ผ๋ก operation์ด buy์ด๋ฉด price๋ฅผ ์์๋ก, sell์ด๋ฉด ์์๋ก ๋ฐํํ๊ณ
sum์ ํด์ capital_gain_loss๋ฅผ ๊ตฌํ ์ ์๋ค.
์์๋ก ๋ณํํ ๋ '-'๋ฅผ ๋ถ์ฌ์ฃผ๊ฑฐ๋ '* -1'์ ๋ถ์ฌ์ค ์ ์๊ฒ ๋ค.
๋๋ ์
์๋ธ์ฟผ๋ฆฌ์์ from์ ์๋ฃ์ด์ ๊ณ์ ์ค๋ฅ๊ฐ ๋ฌ๋ค..ใ
์ ์ ์ ์ฐจ๋ฆฌ์ !

'Tools > - SQL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [SQL] solvesql | ์ ๋ ฅ ์๋น๋ ์ด๋ ํ๊ท ๊ตฌํ๊ธฐ | rows ์ range์ ์ฐจ์ด (0) | 2025.02.09 |
|---|---|
| [SQL] solvesql | ์คํ ๋์ ๋ฌ ์๊ฐ ์ฐพ๊ธฐ (0) | 2025.02.09 |
| [SQL] leetcode | 1045. Customers Who Bought All Products (0) | 2025.02.01 |
| [SQL] leetcode | 1204. Last Person to Fit in the Bus ์์ ํ์ด (0) | 2025.02.01 |
| [SQL] Leetcode | 1907. Count Salary Categories ์์ ํ์ด (0) | 2025.02.01 |