forked from super30admin/Mock_SQL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapples-oranges.sql
86 lines (73 loc) · 2.13 KB
/
apples-oranges.sql
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""
Table: Sales
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| sale_date | date |
| fruit | enum |
| sold_num | int |
+---------------+---------+
(sale_date, fruit) is the primary key (combination of columns with unique values) of this table.
This table contains the sales of "apples" and "oranges" sold each day.
Write a solution to report the difference between the number of apples and oranges sold each day.
Return the result table ordered by sale_date.
The result format is in the following example.
Input:
Sales table:
+------------+------------+-------------+
| sale_date | fruit | sold_num |
+------------+------------+-------------+
| 2020-05-01 | apples | 10 |
| 2020-05-01 | oranges | 8 |
| 2020-05-02 | apples | 15 |
| 2020-05-02 | oranges | 15 |
| 2020-05-03 | apples | 20 |
| 2020-05-03 | oranges | 0 |
| 2020-05-04 | apples | 15 |
| 2020-05-04 | oranges | 16 |
+------------+------------+-------------+
Output:
+------------+--------------+
| sale_date | diff |
+------------+--------------+
| 2020-05-01 | 2 |
| 2020-05-02 | 0 |
| 2020-05-03 | 20 |
| 2020-05-04 | -1 |
+------------+--------------+
"""
-- method 1: using Self join
select
a.sale_date, a.sold_num - b.sold_num as diff
from
Sales a join Sales b
on
a.sale_date = b.sale_date
where
a.fruit = 'apples' and b.fruit = 'oranges'
order by a.sale_date
-- method 2: create two separate tables
select
a.sale_date, a.sold_num - b.sold_num as diff
from
(select sale_date, sold_num from Sales where fruit = 'apples') a
join
(select sale_date, sold_num from Sales where fruit = 'oranges') b
on
a.sale_date = b.sale_date
order by a.sale_date
-- method 3: with SUM(CASE WHEN)
select
sale_date,
sum(
case
when fruit = 'apples' then sold_num
when fruit = 'oranges' then sold_num*-1
end
) as diff
from
Sales
group by
sale_date
order by
sale_date