LeetCode 1393. Capital Gain/Loss
考点 | 难度 |
---|---|
Database | Medium |
题目
Write an SQL query 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.
答案
select stock_name,
sum(case when operation = 'Buy' then -price else price end) as capital_gain_loss
from stocks
group by stock_name