-
Notifications
You must be signed in to change notification settings - Fork 0
/
CIV516_Final_Metrics.sql
92 lines (84 loc) · 2.44 KB
/
CIV516_Final_Metrics.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
/* GET FINAL METRICS FOR CIV516 */
DECLARE target_pattern STRING DEFAULT '52G'; /* INPUT THE BRANCH TO ANALYZE */
/* 1.0 - AVERAGE OVERALL OPERATING SPEED */
SELECT
AVG( operating_speed_kph )
FROM
ts2.oneway_line_operating_speeds
;
/* 1.1 - STRATIFY BY DAY TYPE AND TIME PERIOD */
SELECT
TimePeriod_FromStop, day_type, AVG( operating_speed_kph ) AS avg_op_speed
FROM
`cio-insights-covid-ds13-pr-b0.ts2.oneway_line_operating_speeds_with_dt`
WHERE
operating_speed_kph IS NOT NULL
AND
operating_speed_kph > 0
GROUP BY
TimePeriod_FromStop, day_type
ORDER BY
day_type, TimePeriod_FromStop
;
/* 2.0 - OVERALL STOP TO STOP SEGMENT AVERAGE OPERATING SPEED */
SELECT
AVG(stop2stop_speed_kph ) AS avg_speed
FROM
ts2.stop_to_stop_stats
WHERE
stop2stop_speed_kph IS NOT NULL AND stop2stop_speed_kph > 0
;
/* 2.1 - AVERAGE STOP TO STOP SEGMENT OPERATING SPEED */
SELECT
DepartureStop, ArrivalStop, AVG(stop2stop_speed_kph ) AS avg_speed
FROM
ts2.stop_to_stop_stats
WHERE
stop2stop_speed_kph IS NOT NULL AND stop2stop_speed_kph > 0
GROUP BY
DepartureStop, ArrivalStop
ORDER BY
avg_speed
;
/* 3.0 - Overall On time performance */
DROP TABLE IF EXISTS ts2.stop_otp ;
CREATE TABLE ts2.stop_otp AS
SELECT
avl.DepartureStop ,arrival_adherence_grade, COUNT(*) AS grade_count, ANY_VALUE( total_stops_made ) AS count_total, COUNT(*) / ( ANY_VALUE( total_stops_made ) ) * 100 as grade_percent
FROM
(
SELECT
*,
CASE
WHEN ScheduleAdherence_ArrivalStop_Seconds_ BETWEEN -60 AND 180 THEN 'GRADE A' -- 1 min early to 3 min late
WHEN ScheduleAdherence_ArrivalStop_Seconds_ BETWEEN -120 AND 360 THEN 'GRADE B' -- 2 min early to 6 min late
WHEN ScheduleAdherence_ArrivalStop_Seconds_ BETWEEN -240 AND 720 THEN 'GRADE C' -- 4 min early to 12 min late
ELSE 'GRADE D'
END AS arrival_adherence_grade
FROM
ts2.avl_data
WHERE
PatternName = target_pattern
) avl
INNER JOIN
(
SELECT
DepartureStop, COUNT(*) AS total_stops_made
FROM
ts2.avl_data
GROUP BY
DepartureStop
) totals
ON
avl.DepartureStop = totals.DepartureStop
GROUP BY
DepartureStop, arrival_adherence_grade
;
SELECT
*
FROM
ts2.stop_otp
WHERE
arrival_adherence_grade = 'GRADE D'
ORDER BY
grade_percent DESC