-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
jovenc
committed
Jan 20, 2020
1 parent
666ad6a
commit 7a3118c
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
congestion_data_aggregation/create-table-tt_segments_30min_test.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
CREATE TABLE congestion.tt_segments_30min_test AS | ||
|
||
WITH X AS | ||
( | ||
SELECT a.segment_id, a.link_dir, a.datetime_bin, a.link_length, a.spd_avg_all, a.spd_avg_hc, | ||
a.link_length / a.spd_avg_all * 3.6 AS link_tt_avg_all, | ||
a.link_length / a.spd_avg_hc * 3.6 AS link_tt_avg_hc, | ||
b.length AS seg_length | ||
FROM congestion.speeds_links_30min_test a | ||
INNER JOIN congestion.segments_test b | ||
USING (segment_id) | ||
GROUP BY segment_id, link_dir, datetime_bin, link_length, spd_avg_all, spd_avg_hc, b.length | ||
ORDER BY segment_id, link_dir | ||
) | ||
|
||
, Y AS ( | ||
SELECT segment_id, datetime_bin, | ||
CASE WHEN SUM(link_length) >= 0.8 * seg_length THEN SUM(link_tt_avg_all) * seg_length / SUM(link_length) | ||
END AS segment_tt_avg_all , | ||
SUM(link_length) / seg_length * 100 AS data_pct_all | ||
FROM X | ||
GROUP BY segment_id, datetime_bin, seg_length | ||
ORDER BY segment_id, datetime_bin | ||
) | ||
|
||
, Z AS ( | ||
SELECT segment_id, datetime_bin, | ||
CASE WHEN SUM(link_length) >= 0.8 * seg_length THEN SUM(link_tt_avg_hc) * seg_length / SUM(link_length) | ||
END AS segment_tt_avg_hc , | ||
SUM(link_length) / seg_length * 100 AS data_pct_hc | ||
FROM X | ||
WHERE link_tt_avg_hc IS NOT NULL | ||
GROUP BY segment_id, datetime_bin, seg_length | ||
ORDER BY segment_id, datetime_bin | ||
) | ||
|
||
SELECT Y.segment_id, Y.datetime_bin, | ||
segment_tt_avg_all, data_pct_all, | ||
segment_tt_avg_hc, data_pct_hc | ||
FROM Y | ||
LEFT JOIN Z | ||
USING (segment_id, datetime_bin) |