-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEDA.sql
More file actions
96 lines (87 loc) · 3.48 KB
/
Copy pathEDA.sql
File metadata and controls
96 lines (87 loc) · 3.48 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
use road_trauma;
# Total hospitalisations as per the cause of injury
select cause_of_injury, sum(hospitalisations) as TOTAL_HOSPITALISATIONS from national_monthly_facts
group by cause_of_injury ORDER BY TOTAL_HOSPITALISATIONS DESC;
# Split by age group
select age_group, sum(hospitalisations) as TOTAL_HOSPITALISATIONS from national_monthly_facts
where cause_of_injury = 'Traffic'
group by age_group
ORDER BY TOTAL_HOSPITALISATIONS DESC;
#Comparing Traffic and Non-traffic cases based on the calendyar year
With traffic as(
Select calendar_year, Sum(hospitalisations) as Traffic_hospitalisations
from national_monthly_facts
where cause_of_injury = "Traffic"
group by calendar_year),
non_traffic as (
Select calendar_year, Sum(hospitalisations) as Non_Traffic_hospitalisations
from national_monthly_facts
where cause_of_injury = "Non-Traffic"
group by calendar_year)
Select traffic.calendar_year, Non_Traffic_hospitalisations, Traffic_hospitalisations
from traffic
join non_traffic on traffic.calendar_year = non_traffic.calendar_year
order by traffic.calendar_year;
# Comparison with the previous year values
WITH traffic AS (
SELECT calendar_year, SUM(hospitalisations) AS total_hosp
FROM national_monthly_facts
WHERE cause_of_injury = 'Traffic'
GROUP BY calendar_year
)
SELECT
calendar_year,
total_hosp,
LAG(total_hosp) OVER (ORDER BY calendar_year) AS prev_year_hosp
FROM traffic
ORDER BY calendar_year;
# Calculating year to year growth percentage in traffic hospitalisations
With traffic as(
Select calendar_year, Sum(hospitalisations) as Traffic_hospitalisations
from national_monthly_facts
where cause_of_injury = "Traffic"
group by calendar_year),
with_lag AS(
SELECT calendar_year, Traffic_hospitalisations, LAG(Traffic_hospitalisations) OVER (ORDER BY calendar_year) as prev_year_hospitalisations
from traffic)
SELECT with_lag.calendar_year, Traffic_hospitalisations, prev_year_hospitalisations, ((Traffic_hospitalisations - prev_year_hospitalisations)/prev_year_hospitalisations) * 100 as per_year_growth
from with_lag
order by calendar_year;
#Ranking the traffic hospitalisations as per the road user type
With yearly_road_user AS(
Select calendar_year, road_user, Sum(hospitalisations) as Traffic_hospitalisations
from national_monthly_facts
where cause_of_injury = "Traffic"
group by calendar_year, road_user)
Select calendar_year, road_user, Traffic_hospitalisations,
RANK() OVER (PARTITION BY calendar_year ORDER BY Traffic_hospitalisations DESC) as ranking
FROM yearly_road_user;
# Running total
WITH traffic AS (
SELECT calendar_year, SUM(hospitalisations) AS Traffic_hospitalisations
FROM national_monthly_facts
WHERE cause_of_injury = 'Traffic'
GROUP BY calendar_year
)
SELECT
calendar_year,
Traffic_hospitalisations ,
SUM(Traffic_hospitalisations ) OVER (ORDER BY calendar_year) AS running_total
FROM traffic
ORDER BY calendar_year;
# Sliding Window average of three years
With traffic as (
Select calendar_year, Sum(hospitalisations) as Traffic_hospitalisations
from national_monthly_facts
Where cause_of_injury = "Traffic"
Group BY calendar_year)
Select calendar_year, Traffic_hospitalisations,
AVG(Traffic_hospitalisations) OVER (ORDER BY calendar_year ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) as moving_avg_3_years
From traffic
ORDER BY calendar_year;
# HAVING Clause
Select age_group, Sum(hospitalisations) as Traffic_hospitalisations
from national_monthly_facts
Where cause_of_injury = "Traffic"
GROUP BY age_group
HAVING Traffic_hospitalisations > 40000;