-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7.2.sql
More file actions
144 lines (126 loc) · 3.79 KB
/
7.2.sql
File metadata and controls
144 lines (126 loc) · 3.79 KB
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
-- Exploration
SELECT * FROM vaccinations v;
SELECT * FROM animals a;
SELECT * FROM adoptions a2;
-- SOLUTION STEPS --
-- STEP 1
-- Calculate the vaccinations per year per species
-- STEP 2
-- Calculate the animals per year.
-- a. Calculate the number of admisions per year
-- b. Calculate number of adoptions per year
-- c. Combine the tables with a FULL OUTER JOIN and calculate tha animal variations per year
-- STEP 3
-- Join animals in shelters with vaccinations and calculate the average vaccinations/animal
-- STEP 4
-- Calculate the rolling average and the percentage variation
-- EXECUTION --
-- STEP 1
-- Calculate the vaccinations per year per species
WITH vaccinations_per_year AS (
SELECT
species,
DATE_PART('year',vaccination_time) AS year,
COUNT(*) AS number_of_vaccinations
FROM
vaccinations v
GROUP BY
species,
DATE_PART('year',vaccination_time)
ORDER BY
species
)
-- SELECT * FROM vaccinations_per_year; -- Test
-- STEP 2
-- Calculate the animals per year.
-- a. Calculate the number of admisions per year
-- b. Calculate number of adoptions per year
-- c. Combine the tables with a FULL OUTER JOIN and calculate tha animal variations per year
-- 2a. Admisions per year
, admisions_per_year AS (
SELECT
species,
DATE_PART('year',admission_date) AS year,
COUNT(*) AS number_of_admisions
FROM
animals a1
GROUP BY
species,
DATE_PART('year',admission_date)
ORDER BY
species
)
--SELECT * FROM admisions_per_year; -- Test
-- 2b. Adoptions per year
, adoptions_per_year AS (
SELECT
species,
DATE_PART('year',adoption_date) AS year,
COUNT(*) AS number_of_adoptions
FROM
adoptions a2
GROUP BY
species,
DATE_PART('year',adoption_date)
ORDER BY
species
)
--SELECT * FROM adoptions_per_year; -- Test
-- 2c. Combine the tables with a FULL OUTER JOIN and calculate tha animal variations per year
-- Here I wrapped the fields in COALESCE. This way, if there are no admisions or adoptions in one year, the field still appears
, animals_in_shelters AS (
SELECT
COALESCE(adm.species,adp.species) AS species,
COALESCE (adm.year,adp.year) AS year,
COALESCE(adm.number_of_admisions,0) as number_of_admissions,
COALESCE (adp.number_of_adoptions, 0) AS number_of_adoptions,
(adm.number_of_admisions - COALESCE (adp.number_of_adoptions, 0)) AS animals_in_shelter_variation,
SUM((adm.number_of_admisions - COALESCE (adp.number_of_adoptions, 0)))
OVER ( PARTITION BY adm.species
ORDER BY COALESCE (adm.year,adp.year)
ROWS BETWEEN
UNBOUNDED PRECEDING
AND
CURRENT ROW
) AS animals_in_shelter
FROM admisions_per_year AS adm
FULL OUTER JOIN adoptions_per_year AS adp
ON adm.species = adp.species
AND adm.year = adp.year
)
-- SELECT * FROM animals_in_shelters ; -- Test
-- STEP 3
-- Join animals in shelters with vaccinations and calculate the average vaccinations/animal
, average_vaccinations_per_animal AS (
SELECT
ais.species,
ais."year",
vpy.number_of_vaccinations,
ais.animals_in_shelter,
CAST( (vpy.number_of_vaccinations / ais.animals_in_shelter) AS DECIMAL (5,2) ) AS average_vaccinations_per_animal
FROM animals_in_shelters AS ais
LEFT JOIN vaccinations_per_year AS vpy
ON ais.species = vpy.species
AND ais.year = vpy.year
)
--SELECT * FROM average_vaccinations_per_animal; -- Test
-- STEP 4
-- Calculate the rolling average and the percentage variation
SELECT
species,
"year",
number_of_vaccinations,
-- animals_in_shelter,
average_vaccinations_per_animal,
CAST( AVG(average_vaccinations_per_animal) OVER w AS DECIMAL (5,2) ) AS previous_2_years_average,
CAST( (average_vaccinations_per_animal / AVG(average_vaccinations_per_animal) OVER w) *100 AS DECIMAL (5,2) ) AS percent_change
FROM average_vaccinations_per_animal
WINDOW w AS (
PARTITION BY species
ORDER BY year
RANGE BETWEEN
2 PRECEDING
AND
1 PRECEDING
)
;