Skip to content

Commit

Permalink
#66 update baseline function
Browse files Browse the repository at this point in the history
  • Loading branch information
chmnata committed Oct 12, 2022
1 parent f7d0cdb commit 3705d1d
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions data_aggregation/sql/function_generate_centreline_baseline.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ AS $BODY$
WITH link_60_tt AS (
SELECT segment_id,
link_dir,
dt,
ta.dt,
extract(hour from tod)::int AS hr,
avg(links.length * 0.001/ mean * 3600) AS link_tt,
links.length
Expand All @@ -22,40 +22,42 @@ WITH link_60_tt AS (
date_part('isodow'::text, ta.dt)::integer < 6 AND -- include only weekdays
confidence >= 30 -- only use high confidence data

GROUP BY segment_id, link_dir, dt, hr, links.length)
GROUP BY segment_id, link_dir, ta.dt, hr, links.length)

, segment_60_tt AS (
SELECT segment_id,
dt,
hr,
total_length / (sum(link_60_tt.length) / sum(link_60_tt.link_tt)) AS segment_tt_avg
total_length / (sum(link_60_tt.length) / sum(link_60_tt.link_tt)) AS segment_tt_avg,
total_length AS seg_length

FROM link_60_tt
INNER JOIN congestion.network_segments USING (segment_id)

GROUP BY dt, hr, segment_id, total_length
HAVING sum(link_60_tt.length) >= (total_length * 0.8))-- where at least 80% of links have data

, centreline_60_tt AS (
, centreline_set_60_tt AS (
SELECT uid,
dt,
hr,
length / (sum(total_length) / sum(segment_tt_avg)) as tt
((segment_set_length / (sum(seg_length) / sum(segment_tt_avg))) * cent_length)/segment_set_length as tt

FROM segment_60_tt
INNER JOIN congestion.segment_centreline_lookup
GROUP BY uid, dt, hr
HAVING sum(total_length) >= (length * 0.8))
INNER JOIN congestion.segment_centreline_lookup USING (segment_id)
GROUP BY uid, dt, hr, segment_set_length, cent_length
HAVING sum(seg_length) >= (segment_set_length * 0.8))

INSERT INTO congestion.centreline_baseline
SELECT uid,
_yr AS yr,
PERCENTILE_CONT (0.10) WITHIN GROUP (ORDER BY segment_tt_avg ASC) AS baseline_10pct,
PERCENTILE_CONT (0.15) WITHIN GROUP (ORDER BY segment_tt_avg ASC) AS baseline_15pct,
PERCENTILE_CONT (0.20) WITHIN GROUP (ORDER BY segment_tt_avg ASC) AS baseline_20pct,
PERCENTILE_CONT (0.25) WITHIN GROUP (ORDER BY segment_tt_avg ASC) AS baseline_25pct
PERCENTILE_CONT (0.10) WITHIN GROUP (ORDER BY tt ASC) AS baseline_10pct,
PERCENTILE_CONT (0.15) WITHIN GROUP (ORDER BY tt ASC) AS baseline_15pct,
PERCENTILE_CONT (0.20) WITHIN GROUP (ORDER BY tt ASC) AS baseline_20pct,
PERCENTILE_CONT (0.25) WITHIN GROUP (ORDER BY tt ASC) AS baseline_25pct

FROM centreline_60_tt
WHERE datetime_bin::time >= '07:00:00' AND datetime_bin::time < '21:00:00'
FROM centreline_set_60_tt
WHERE hr>= 7 AND hr < 21
GROUP BY uid;

$BODY$;
Expand All @@ -66,4 +68,7 @@ ALTER FUNCTION congestion.generate_centreline_monthly(date)
GRANT EXECUTE ON FUNCTION congestion.generate_centreline_monthly(date) TO congestion_admins;
GRANT EXECUTE ON FUNCTION congestion.generate_centreline_monthly(date) TO congestion_bot;
COMMENT ON FUNCTION congestion.generate_centreline_monthly(date)
IS 'Function that aggregate centreline equivalent of network segments baseline travel time for each year, excluding holidays.';
IS 'Function that aggregate centreline equivalent of network segments baseline travel time for each year, excluding holidays.';



0 comments on commit 3705d1d

Please sign in to comment.