-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path61.sql
More file actions
68 lines (59 loc) · 3 KB
/
61.sql
File metadata and controls
68 lines (59 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
-- Question 61
-- Table: Stocks
-- +---------------+---------+
-- | Column Name | Type |
-- +---------------+---------+
-- | stock_name | varchar |
-- | operation | enum |
-- | operation_day | int |
-- | price | int |
-- +---------------+---------+
-- (stock_name, day) is the primary key for this table.
-- The operation column is an ENUM of type ('Sell', 'Buy')
-- Each row of this table indicates that the stock which has stock_name had an operation on the day operation_day with the price.
-- It is guaranteed that each 'Sell' operation for a stock has a corresponding 'Buy' operation in a previous day.
-- Write an SQL query to report the Capital gain/loss for each stock.
-- The capital gain/loss of a stock is total gain or loss after buying and selling the stock one or many times.
-- Return the result table in any order.
-- The query result format is in the following example:
-- Stocks table:
-- +---------------+-----------+---------------+--------+
-- | stock_name | operation | operation_day | price |
-- +---------------+-----------+---------------+--------+
-- | Leetcode | Buy | 1 | 1000 |
-- | Corona Masks | Buy | 2 | 10 |
-- | Leetcode | Sell | 5 | 9000 |
-- | Handbags | Buy | 17 | 30000 |
-- | Corona Masks | Sell | 3 | 1010 |
-- | Corona Masks | Buy | 4 | 1000 |
-- | Corona Masks | Sell | 5 | 500 |
-- | Corona Masks | Buy | 6 | 1000 |
-- | Handbags | Sell | 29 | 7000 |
-- | Corona Masks | Sell | 10 | 10000 |
-- +---------------+-----------+---------------+--------+
-- Result table:
-- +---------------+-------------------+
-- | stock_name | capital_gain_loss |
-- +---------------+-------------------+
-- | Corona Masks | 9500 |
-- | Leetcode | 8000 |
-- | Handbags | -23000 |
-- +---------------+-------------------+
-- Leetcode stock was bought at day 1 for 1000$ and was sold at day 5 for 9000$. Capital gain = 9000 - 1000 = 8000$.
-- Handbags stock was bought at day 17 for 30000$ and was sold at day 29 for 7000$. Capital loss = 7000 - 30000 = -23000$.
-- Corona Masks stock was bought at day 1 for 10$ and was sold at day 3 for 1010$. It was bought again at day 4 for 1000$ and was sold at day 5 for 500$. At last, it was bought at day 6 for 1000$ and was sold at day 10 for 10000$. Capital gain/loss is the sum of capital gains/losses for each ('Buy' --> 'Sell')
-- operation = (1010 - 10) + (500 - 1000) + (10000 - 1000) = 1000 - 500 + 9000 = 9500$.
-- Solution
select stock_name, (sell_total-buy_total) as capital_gain_loss
from(
(select stock_name, sum(price) as sell_total
from stocks
where operation = 'Sell'
group by stock_name) b
left join
(select stock_name as name, sum(price) as buy_total
from stocks
where operation = 'Buy'
group by stock_name) c
on b.stock_name = c.name)
order by capital_gain_loss desc