-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.4.sql
126 lines (121 loc) · 3.69 KB
/
3.4.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
-- Start with a simple join
SELECT *
FROM Vaccinations AS V
INNER JOIN
Persons AS P
ON P.Email = V.Email;
-- Add grouping sets and required columns (doesn't work yet)
SELECT YEAR(V.Vaccination_Time) AS Year,
V.Species,
V.Email,
P.First_Name,
P.Last_Name,
COUNT(*) AS Number_Of_Vaccinations,
MAX(YEAR(V.Vaccination_Time)) AS Latest_Vaccination_Year -- yes, this is legit!
FROM Vaccinations AS V
INNER JOIN
Persons AS P
ON P.Email = V.Email
GROUP BY GROUPING SETS (
(),
YEAR(V.Vaccination_Time),
V.Species,
(YEAR(V.Vaccination_Time), V.Species),
V.Email,
(V.Email, V.Species)
);
-- Try to add dummy aggregates for first name and last name
SELECT YEAR(V.Vaccination_Time) AS Year,
V.Species,
V.Email,
MAX(P.First_Name) AS First_Name, -- Dummy aggregate
MAX(P.Last_Name) AS Last_Name, -- Dummy aggregate
COUNT(*) AS Number_Of_Vaccinations,
MAX(YEAR(V.Vaccination_Time)) AS Latest_Vaccination_Year
FROM Vaccinations AS V
INNER JOIN
Persons AS P
ON P.Email = V.Email
GROUP BY GROUPING SETS (
(),
YEAR(V.Vaccination_Time),
V.Species,
(YEAR(V.Vaccination_Time), V.Species),
(V.Email),
(V.Email, V.Species)
);
-- Add NULL replacement with COALESCE, but what's wrong with first name and last name???
SELECT COALESCE(CAST(YEAR(V.Vaccination_Time) AS VARCHAR(10)), 'All Years') AS Year,
COALESCE(V.Species, 'All Species') AS Species,
COALESCE(V.Email, 'All Staff') AS Email,
COALESCE(MAX(P.First_Name), '') AS First_Name, -- Dummy aggregate
COALESCE(MAX(P.Last_Name), '') AS Last_Name, -- Dummy aggregate
COUNT(*) AS Number_Of_Vaccinations,
MAX(YEAR(V.Vaccination_Time)) AS Latest_Vaccination_Year
FROM Vaccinations AS V
INNER JOIN
Persons AS P
ON P.Email = V.Email
GROUP BY GROUPING SETS (
(),
YEAR(V.Vaccination_Time),
V.Species,
(YEAR(V.Vaccination_Time), V.Species),
(V.Email),
(V.Email, V.Species)
);
-- Must use the GROUPING function to distinguish "All staff" from individuals
SELECT COALESCE(CAST(YEAR(V.Vaccination_Time) AS VARCHAR(10)), 'All Years') AS Year,
COALESCE(V.Species, 'All Species') AS Species,
COALESCE(V.Email, 'All Staff') AS Email,
CASE WHEN GROUPING(V.Email) = 0
THEN MAX(P.First_Name) -- Dummy aggregate
ELSE ''
END AS First_Name,
CASE WHEN GROUPING(V.Email) = 0
THEN MAX(P.Last_Name) -- Dummy aggregate
ELSE ''
END AS Last_Name,
COUNT(*) AS Number_Of_Vaccinations,
MAX(YEAR(V.Vaccination_Time)) AS Latest_Vaccination_Year
FROM Vaccinations AS V
INNER JOIN
Persons AS P
ON P.Email = V.Email
GROUP BY GROUPING SETS (
(),
YEAR(V.Vaccination_Time),
V.Species,
(YEAR(V.Vaccination_Time), V.Species),
(V.Email),
(V.Email, V.Species)
)
ORDER BY Year, Species, First_Name, Last_Name;
/* PostgreSQL
SELECT COALESCE(CAST(EXTRACT(YEAR FROM V.Vaccination_Time) AS VARCHAR(10)), 'All Years') AS Year,
COALESCE(V.Species, 'All Species') AS Species,
COALESCE(V.Email, 'All Staff') AS Email,
CASE WHEN GROUPING(V.Email) = 0
THEN MAX(P.First_Name) -- Dummy aggregate
ELSE ' '
END AS First_Name,
CASE WHEN GROUPING(V.Email) = 0
THEN MAX(P.Last_Name) -- Dummy aggregate
ELSE ' '
END AS Last_Name,
COUNT(*) AS Number_Of_Vaccinations,
MAX(EXTRACT(YEAR FROM V.Vaccination_Time)) AS Latest_Vaccination_Year
FROM Vaccinations AS V
INNER JOIN
Persons AS P
ON P.Email = V.Email
GROUP BY GROUPING SETS (
(),
EXTRACT(YEAR FROM V.Vaccination_Time),
V.Species,
(EXTRACT(YEAR FROM V.Vaccination_Time), V.Species),
(V.Email),
(V.Email, V.Species)
)
ORDER BY Year, V.Species NULLS FIRST, First_Name, Last_Name;
*/