Skip to content

Commit fe12460

Browse files
committed
Upload 151
1 parent 73eb36d commit fe12460

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ The problems and both submitted and site solutions are documented in individual
183183
| 148 | [Swap Salary](https://leetcode.com/problems/swap-salary/description/) | [Solution](solutions/148_swap_salary.md) | LeetCode | Easy | |
184184
| 149 | [Sales Person](https://leetcode.com/problems/sales-person/description/) | [Solution](solutions/149_sales_person.md) | LeetCode | Easy | |
185185
| 150 | [Reformat Department Table](https://leetcode.com/problems/reformat-department-table/description/) | [Solution](solutions/150_reformat_department_table.md) | LeetCode | Easy | |
186+
| 151 | [Capital Gain/Loss](https://leetcode.com/problems/capital-gainloss/description/) | [Solution](solutions/151_capital_gainloss.md) | LeetCode | Medium | |
186187
<!-- Index End - WARNING: Do not delete or modify this markdown comment. -->
187188

188189
## Author(s)

solutions/151_capital_gainloss.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# SQL Everyday \#151
2+
3+
## Capital Gain/Loss
4+
5+
Site: LeetCode\
6+
Difficulty per Site: Medium
7+
8+
## Problem
9+
10+
Write a solution to report the *Capital gain/loss* for each stock.
11+
12+
The *Capital gain/loss* of a stock is the total gain or loss after buying and selling the stock one or many times.
13+
14+
Return the result table in *any order*. [[Full Description](https://leetcode.com/problems/capital-gainloss/description/)]
15+
16+
## Submitted Solution
17+
18+
```sql
19+
-- Submitted Solution
20+
WITH cte AS (
21+
SELECT
22+
stock_name
23+
,CASE WHEN operation = 'Buy' THEN -price ELSE price END AS new_price
24+
FROM Stocks
25+
)
26+
SELECT
27+
stock_name
28+
,SUM(new_price) AS capital_gain_loss
29+
FROM cte
30+
GROUP BY stock_name
31+
;
32+
```
33+
34+
## Site Solution
35+
36+
```sql
37+
-- LeetCode Solution
38+
SELECT
39+
stock_name,
40+
SUM(
41+
CASE
42+
WHEN operation = 'buy' THEN -price
43+
WHEN operation = 'sell' THEN price
44+
END
45+
) AS capital_gain_loss
46+
FROM Stocks
47+
GROUP BY stock_name
48+
```
49+
50+
## Notes
51+
52+
TBD
53+
54+
## NB
55+
56+
TBD
57+
58+
Go to [Index](../?tab=readme-ov-file#index)\
59+
Go to [Overview](../?tab=readme-ov-file)

0 commit comments

Comments
 (0)