-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.2.sql
225 lines (209 loc) · 4.67 KB
/
3.2.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
-- Multi level aggregates
-- Number of annual, monthly, and overall adoptions.
SELECT YEAR(Adoption_Date) AS Year,
MONTH(Adoption_Date) AS Month,
COUNT(*) AS Monthly_Adoptions
FROM Adoptions
GROUP BY YEAR(Adoption_Date), MONTH(Adoption_Date);
SELECT YEAR(Adoption_Date) AS Year,
COUNT(*) AS Annual_Adoptions
FROM Adoptions
GROUP BY YEAR(Adoption_Date);
SELECT COUNT(*) AS Total_Adoptions
FROM Adoptions
GROUP BY ();
-- Add UNION ALL... no good
SELECT YEAR(Adoption_Date) AS Year,
MONTH(Adoption_Date) AS Month,
COUNT(*) AS Number_Of_Adoptions
FROM Adoptions
GROUP BY YEAR(Adoption_Date), MONTH(Adoption_Date)
UNION ALL
SELECT YEAR(Adoption_Date) AS Year,
COUNT(*) AS Annual_Adoptions
FROM Adoptions
GROUP BY YEAR(Adoption_Date)
UNION ALL
SELECT COUNT(*) AS Total_Adoptions
FROM Adoptions
GROUP BY ();
-- Try string placeholders... no good
SELECT YEAR(Adoption_Date) AS Year,
MONTH(Adoption_Date) AS Month,
COUNT(*) AS Number_Of_Adoptions
FROM Adoptions
GROUP BY YEAR(Adoption_Date), MONTH(Adoption_Date)
UNION ALL
SELECT YEAR(Adoption_Date) AS Year,
'All Months' AS Month,
COUNT(*) AS Annual_Adoptions
FROM Adoptions
GROUP BY YEAR(Adoption_Date)
UNION ALL
SELECT 'All Years' AS Year,
'All Months' AS Month,
COUNT(*) AS Total_Adoptions
FROM Adoptions
GROUP BY ()
ORDER BY Year, Month;
-- Use NULL placeholders... very good!
SELECT YEAR(Adoption_Date) AS Year,
MONTH(Adoption_Date) AS Month,
COUNT(*) AS Monthly_Adoptions
FROM Adoptions
GROUP BY YEAR(Adoption_Date), MONTH(Adoption_Date)
UNION ALL
SELECT YEAR(Adoption_Date) AS Year,
NULL AS Month,
COUNT(*) AS Annual_Adoptions
FROM Adoptions
GROUP BY YEAR(Adoption_Date)
UNION ALL
SELECT NULL AS Year,
NULL AS Month,
COUNT(*) AS Total_Adoptions
FROM Adoptions
GROUP BY ()
ORDER BY Year, Month;
-- Reuse lowest granularity aggregate in WITH clause
WITH Aggregated_Adoptions
AS
(
SELECT YEAR(Adoption_Date) AS Year,
MONTH(Adoption_Date) AS Month,
COUNT(*) AS Monthly_Adoptions
FROM Adoptions
GROUP BY YEAR(Adoption_Date), MONTH(Adoption_Date)
)
SELECT *
FROM Aggregated_Adoptions
UNION ALL
SELECT Year,
NULL,
COUNT(*)
FROM Aggregated_Adoptions
GROUP BY Year
UNION ALL
SELECT NULL,
NULL,
COUNT(*)
FROM Aggregated_Adoptions
GROUP BY ();
/* PostgreSQL
-- Reuse lowest granularity aggregate in WITH clause
WITH Aggregated_Adoptions
AS
(
SELECT EXTRACT(year FROM Adoption_Date) AS Year,
EXTRACT(month FROM Adoption_Date) AS Month,
COUNT(*) AS Monthly_Adoptions
FROM Adoptions
GROUP BY EXTRACT(year FROM Adoption_Date) , EXTRACT(month FROM Adoption_Date)
)
SELECT *
FROM Aggregated_Adoptions
UNION ALL
SELECT Year,
NULL,
COUNT(*)
FROM Aggregated_Adoptions
GROUP BY Year
UNION ALL
SELECT NULL,
NULL,
COUNT(*)
FROM Aggregated_Adoptions
GROUP BY ();
*/
-- GROUPING SETS
-- Equivalent to no GROUP BY
SELECT COUNT(*) AS Total_Adoptions
FROM Adoptions
GROUP BY GROUPING SETS
(
()
);
-- Equivalent to GROUP BY YEAR(Adoption_Date)
SELECT YEAR(Adoption_Date) AS Year,
COUNT(*) AS Annual_Adoptions
FROM Adoptions
GROUP BY GROUPING SETS
(
YEAR(Adoption_Date)
)
ORDER BY Year;
-- Equivalent to GROUP BY YEAR(Adoption_Date), MONTH(Adoption_Date)
SELECT YEAR(Adoption_Date) AS Year,
MONTH(Adoption_Date) AS Month,
COUNT(*) AS Monthly_Adoptions
FROM Adoptions
GROUP BY GROUPING SETS
(
(
YEAR(Adoption_Date), MONTH(Adoption_Date)
)
)
ORDER BY Year, Month;
-- Be careful with the parentheses!
SELECT YEAR(Adoption_Date) AS Year,
MONTH(Adoption_Date) AS Month,
COUNT(*) AS Monthly_Adoptions
FROM Adoptions
GROUP BY GROUPING SETS
(
YEAR(Adoption_Date), MONTH(Adoption_Date)
)
ORDER BY Year, Month;
-- All in one...
SELECT YEAR(Adoption_Date) AS Year,
MONTH(Adoption_Date) AS Month,
COUNT(*) AS Monthly_Adoptions
FROM Adoptions
GROUP BY GROUPING SETS
(
(YEAR(Adoption_Date), MONTH(Adoption_Date)),
YEAR(Adoption_Date),
()
)
ORDER BY Year, Month;
/* PostgreSQL
-- All in one...
SELECT EXTRACT(year FROM Adoption_Date) AS Year,
EXTRACT(month FROM Adoption_Date) AS Month,
COUNT(*) AS Monthly_Adoptions
FROM Adoptions
GROUP BY GROUPING SETS
(
(EXTRACT(year FROM Adoption_Date), extract(month FROM Adoption_Date)),
EXTRACT(year FROM Adoption_Date),
()
)
ORDER BY Year, Month;
*/
-- Non hierarchical grouping sets
SELECT YEAR(Adoption_Date) AS Year,
Adopter_Email,
COUNT(*) AS Annual_Adoptions
FROM Adoptions
GROUP BY GROUPING SETS
(
YEAR(Adoption_Date),
Adopter_Email
);
-- Handling NULLs
SELECT COALESCE(Species, 'All') AS Species,
CASE
WHEN GROUPING(Breed) = 1
THEN 'All'
ELSE Breed
END AS Breed,
GROUPING(Breed) AS Is_This_All_Breeds,
COUNT(*) AS Number_Of_Animals
FROM Animals
GROUP BY GROUPING SETS
(
Species,
Breed,
()
)
ORDER BY Species, Breed;