-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2021.01.17.sql
111 lines (92 loc) · 2.34 KB
/
2021.01.17.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
DROP TABLE IF EXISTS sales;
CREATE TABLE sales (brand varchar NOT NULL,
SEGMENT varchar NOT NULL,
quantity int NOT NULL,
PRIMARY KEY (brand,
SEGMENT));
INSERT INTO sales (brand, SEGMENT, quantity)
VALUES ('ABC', 'Premium', 100),
('ABC', 'Basic', 200),
('XYZ', 'Premium', 100),
('XYZ', 'Basic', 300);
SELECT brand,
SEGMENT,
sum(quantity)
FROM sales
GROUP BY ROLLUP (brand,
SEGMENT)
ORDER BY brand,
SEGMENT;
SELECT SEGMENT,
brand,
sum(quantity)
FROM sales
GROUP BY ROLLUP (SEGMENT,
brand)
ORDER BY SEGMENT,
brand;
SELECT SEGMENT,
brand,
sum(quantity)
FROM sales
GROUP BY SEGMENT,
ROLLUP (brand)-- brand
ORDER BY SEGMENT,
brand;
SELECT extract(YEAR
FROM rental_date) y,
extract(MONTH
FROM rental_date) m,
extract(DAY
FROM rental_date) d,
count(rental_id)
FROM rental
GROUP BY ROLLUP (extract(YEAR
FROM rental_date),
extract(MONTH
FROM rental_date),
extract(DAY
FROM rental_date));
SELECT extract(YEAR
FROM rental_date) y,
extract(MONTH
FROM rental_date) m,
extract(DAY
FROM rental_date) d,
count(rental_id)
FROM rental
GROUP BY ROLLUP (y,
m,
d)
ORDER BY y,
m,
d;
SELECT extract(YEAR
FROM rental_date) y,
extract(MONTH
FROM rental_date) m,
extract(DAY
FROM rental_date) d,
sum(amount)
FROM rental
INNER JOIN payment USING (rental_id)
GROUP BY ROLLUP (y,
m,
d)
ORDER BY y,
m,
d;
SELECT CASE
WHEN brand NOTNULL THEN brand
ELSE '<ALL BRANDS>'
END AS brand,
CASE
WHEN SEGMENT NOTNULL THEN SEGMENT
ELSE '<ALL SEGMENTS>'
END AS SEGMENT,
sum(quantity)
FROM sales
GROUP BY ROLLUP (brand,
SEGMENT)
ORDER BY brand,
SEGMENT;