From 053f9608e23c22cc02973335bfefd0fa950e07d6 Mon Sep 17 00:00:00 2001 From: Gabe Wolofsky <80077912+gabrielwol@users.noreply.github.com> Date: Wed, 10 Jul 2024 11:49:51 -0400 Subject: [PATCH] #905 sqlfluff --- .../create-view-miovision_wide_tmc.sql | 326 ++++++++++++------ 1 file changed, 224 insertions(+), 102 deletions(-) diff --git a/volumes/miovision/sql/open_data/create-view-miovision_wide_tmc.sql b/volumes/miovision/sql/open_data/create-view-miovision_wide_tmc.sql index f507cbc5e..0886322f4 100644 --- a/volumes/miovision/sql/open_data/create-view-miovision_wide_tmc.sql +++ b/volumes/miovision/sql/open_data/create-view-miovision_wide_tmc.sql @@ -10,145 +10,261 @@ CREATE OR REPLACE VIEW gwolofs.miovision_open_data_wide_15min AS ( SELECT v.intersection_uid, v.datetime_bin, - COALESCE(SUM(v.volume) FILTER (WHERE - v.movement_uid = 3 AND v.classification_uid = ANY(ARRAY[1]::int []) AND v.leg = 'S' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.movement_uid = 3 + AND v.classification_uid = ANY(ARRAY[1]::int []) + AND v.leg = 'S' ), 0) AS sb_car_r, - COALESCE(SUM(v.volume) FILTER (WHERE - v.movement_uid = 1 AND v.classification_uid = ANY(ARRAY[1]::int []) AND v.leg = 'S' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.movement_uid = 1 + AND v.classification_uid = ANY(ARRAY[1]::int []) + AND v.leg = 'S' ), 0) AS sb_car_t, - COALESCE(SUM(v.volume) FILTER (WHERE - v.movement_uid = 2 AND v.classification_uid = ANY(ARRAY[1]::int []) AND v.leg = 'S' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.movement_uid = 2 + AND v.classification_uid = ANY(ARRAY[1]::int []) + AND v.leg = 'S' ), 0) AS sb_car_l, - COALESCE(SUM(v.volume) FILTER (WHERE - v.movement_uid = 3 AND v.classification_uid = ANY(ARRAY[1]::int []) AND v.leg = 'N' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.movement_uid = 3 + AND v.classification_uid = ANY(ARRAY[1]::int []) + AND v.leg = 'N' ), 0) AS nb_car_r, - COALESCE(SUM(v.volume) FILTER (WHERE - v.movement_uid = 1 AND v.classification_uid = ANY(ARRAY[1]::int []) AND v.leg = 'N' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.movement_uid = 1 + AND v.classification_uid = ANY(ARRAY[1]::int []) + AND v.leg = 'N' ), 0) AS nb_car_t, - COALESCE(SUM(v.volume) FILTER (WHERE - v.movement_uid = 2 AND v.classification_uid = ANY(ARRAY[1]::int []) AND v.leg = 'N' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.movement_uid = 2 + AND v.classification_uid = ANY(ARRAY[1]::int []) + AND v.leg = 'N' ), 0) AS nb_car_l, - COALESCE(SUM(v.volume) FILTER (WHERE - v.movement_uid = 3 AND v.classification_uid = ANY(ARRAY[1]::int []) AND v.leg = 'W' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.movement_uid = 3 + AND v.classification_uid = ANY(ARRAY[1]::int []) + AND v.leg = 'W' ), 0) AS wb_car_r, - COALESCE(SUM(v.volume) FILTER (WHERE - v.movement_uid = 1 AND v.classification_uid = ANY(ARRAY[1]::int []) AND v.leg = 'W' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.movement_uid = 1 + AND v.classification_uid = ANY(ARRAY[1]::int []) + AND v.leg = 'W' ), 0) AS wb_car_t, - COALESCE(SUM(v.volume) FILTER (WHERE - v.movement_uid = 2 AND v.classification_uid = ANY(ARRAY[1]::int []) AND v.leg = 'W' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.movement_uid = 2 + AND v.classification_uid = ANY(ARRAY[1]::int []) + AND v.leg = 'W' ), 0) AS wb_car_l, - COALESCE(SUM(v.volume) FILTER (WHERE - v.movement_uid = 3 AND v.classification_uid = ANY(ARRAY[1]::int []) AND v.leg = 'E' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.movement_uid = 3 + AND v.classification_uid = ANY(ARRAY[1]::int []) + AND v.leg = 'E' ), 0) AS eb_car_r, - COALESCE(SUM(v.volume) FILTER (WHERE - v.movement_uid = 1 AND v.classification_uid = ANY(ARRAY[1]::int []) AND v.leg = 'E' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.movement_uid = 1 + AND v.classification_uid = ANY(ARRAY[1]::int []) + AND v.leg = 'E' ), 0) AS eb_car_t, - COALESCE(SUM(v.volume) FILTER (WHERE - v.movement_uid = 2 AND v.classification_uid = ANY(ARRAY[1]::int []) AND v.leg = 'E' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.movement_uid = 2 + AND v.classification_uid = ANY(ARRAY[1]::int []) + AND v.leg = 'E' ), 0) AS eb_car_l, - COALESCE(SUM(v.volume) FILTER (WHERE - v.movement_uid = 3 AND v.classification_uid = ANY(ARRAY[4, 5, 9]::int []) AND v.leg = 'S' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.movement_uid = 3 + AND v.classification_uid = ANY(ARRAY[4, 5, 9]::int []) + AND v.leg = 'S' ), 0) AS sb_truck_r, - COALESCE(SUM(v.volume) FILTER (WHERE - v.movement_uid = 1 AND v.classification_uid = ANY(ARRAY[4, 5, 9]::int []) AND v.leg = 'S' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.movement_uid = 1 + AND v.classification_uid = ANY(ARRAY[4, 5, 9]::int []) + AND v.leg = 'S' ), 0) AS sb_truck_t, - COALESCE(SUM(v.volume) FILTER (WHERE - v.movement_uid = 2 AND v.classification_uid = ANY(ARRAY[4, 5, 9]::int []) AND v.leg = 'S' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.movement_uid = 2 + AND v.classification_uid = ANY(ARRAY[4, 5, 9]::int []) + AND v.leg = 'S' ), 0) AS sb_truck_l, - COALESCE(SUM(v.volume) FILTER (WHERE - v.movement_uid = 3 AND v.classification_uid = ANY(ARRAY[4, 5, 9]::int []) AND v.leg = 'N' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.movement_uid = 3 + AND v.classification_uid = ANY(ARRAY[4, 5, 9]::int []) + AND v.leg = 'N' ), 0) AS nb_truck_r, - COALESCE(SUM(v.volume) FILTER (WHERE - v.movement_uid = 1 AND v.classification_uid = ANY(ARRAY[4, 5, 9]::int []) AND v.leg = 'N' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.movement_uid = 1 + AND v.classification_uid = ANY(ARRAY[4, 5, 9]::int []) + AND v.leg = 'N' ), 0) AS nb_truck_t, - COALESCE(SUM(v.volume) FILTER (WHERE - v.movement_uid = 2 AND v.classification_uid = ANY(ARRAY[4, 5, 9]::int []) AND v.leg = 'N' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.movement_uid = 2 + AND v.classification_uid = ANY(ARRAY[4, 5, 9]::int []) + AND v.leg = 'N' ), 0) AS nb_truck_l, - COALESCE(SUM(v.volume) FILTER (WHERE - v.movement_uid = 3 AND v.classification_uid = ANY(ARRAY[4, 5, 9]::int []) AND v.leg = 'W' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.movement_uid = 3 + AND v.classification_uid = ANY(ARRAY[4, 5, 9]::int []) + AND v.leg = 'W' ), 0) AS wb_truck_r, - COALESCE(SUM(v.volume) FILTER (WHERE - v.movement_uid = 1 AND v.classification_uid = ANY(ARRAY[4, 5, 9]::int []) AND v.leg = 'W' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.movement_uid = 1 + AND v.classification_uid = ANY(ARRAY[4, 5, 9]::int []) + AND v.leg = 'W' ), 0) AS wb_truck_t, - COALESCE(SUM(v.volume) FILTER (WHERE - v.movement_uid = 2 AND v.classification_uid = ANY(ARRAY[4, 5, 9]::int []) AND v.leg = 'W' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.movement_uid = 2 + AND v.classification_uid = ANY(ARRAY[4, 5, 9]::int []) + AND v.leg = 'W' ), 0) AS wb_truck_l, - COALESCE(SUM(v.volume) FILTER (WHERE - v.movement_uid = 3 AND v.classification_uid = ANY(ARRAY[4, 5, 9]::int []) AND v.leg = 'E' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.movement_uid = 3 + AND v.classification_uid = ANY(ARRAY[4, 5, 9]::int []) + AND v.leg = 'E' ), 0) AS eb_truck_r, - COALESCE(SUM(v.volume) FILTER (WHERE - v.movement_uid = 1 AND v.classification_uid = ANY(ARRAY[4, 5, 9]::int []) AND v.leg = 'E' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.movement_uid = 1 + AND v.classification_uid = ANY(ARRAY[4, 5, 9]::int []) + AND v.leg = 'E' ), 0) AS eb_truck_t, - COALESCE(SUM(v.volume) FILTER (WHERE - v.movement_uid = 2 AND v.classification_uid = ANY(ARRAY[4, 5, 9]::int []) AND v.leg = 'E' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.movement_uid = 2 + AND v.classification_uid = ANY(ARRAY[4, 5, 9]::int []) + AND v.leg = 'E' ), 0) AS eb_truck_l, - COALESCE(SUM(v.volume) FILTER (WHERE - v.movement_uid = 3 AND v.classification_uid = ANY(ARRAY[3]::int []) AND v.leg = 'S' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.movement_uid = 3 + AND v.classification_uid = ANY(ARRAY[3]::int []) + AND v.leg = 'S' ), 0) AS sb_bus_r, - COALESCE(SUM(v.volume) FILTER (WHERE - v.movement_uid = 1 AND v.classification_uid = ANY(ARRAY[3]::int []) AND v.leg = 'S' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.movement_uid = 1 + AND v.classification_uid = ANY(ARRAY[3]::int []) + AND v.leg = 'S' ), 0) AS sb_bus_t, - COALESCE(SUM(v.volume) FILTER (WHERE - v.movement_uid = 2 AND v.classification_uid = ANY(ARRAY[3]::int []) AND v.leg = 'S' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.movement_uid = 2 + AND v.classification_uid = ANY(ARRAY[3]::int []) + AND v.leg = 'S' ), 0) AS sb_bus_l, - COALESCE(SUM(v.volume) FILTER (WHERE - v.movement_uid = 3 AND v.classification_uid = ANY(ARRAY[3]::int []) AND v.leg = 'N' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.movement_uid = 3 + AND v.classification_uid = ANY(ARRAY[3]::int []) + AND v.leg = 'N' ), 0) AS nb_bus_r, - COALESCE(SUM(v.volume) FILTER (WHERE - v.movement_uid = 1 AND v.classification_uid = ANY(ARRAY[3]::int []) AND v.leg = 'N' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.movement_uid = 1 + AND v.classification_uid = ANY(ARRAY[3]::int []) + AND v.leg = 'N' ), 0) AS nb_bus_t, - COALESCE(SUM(v.volume) FILTER (WHERE - v.movement_uid = 2 AND v.classification_uid = ANY(ARRAY[3]::int []) AND v.leg = 'N' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.movement_uid = 2 + AND v.classification_uid = ANY(ARRAY[3]::int []) + AND v.leg = 'N' ), 0) AS nb_bus_l, - COALESCE(SUM(v.volume) FILTER (WHERE - v.movement_uid = 3 AND v.classification_uid = ANY(ARRAY[3]::int []) AND v.leg = 'W' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.movement_uid = 3 + AND v.classification_uid = ANY(ARRAY[3]::int []) + AND v.leg = 'W' ), 0) AS wb_bus_r, - COALESCE(SUM(v.volume) FILTER (WHERE - v.movement_uid = 1 AND v.classification_uid = ANY(ARRAY[3]::int []) AND v.leg = 'W' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.movement_uid = 1 + AND v.classification_uid = ANY(ARRAY[3]::int []) + AND v.leg = 'W' ), 0) AS wb_bus_t, - COALESCE(SUM(v.volume) FILTER (WHERE - v.movement_uid = 2 AND v.classification_uid = ANY(ARRAY[3]::int []) AND v.leg = 'W' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.movement_uid = 2 + AND v.classification_uid = ANY(ARRAY[3]::int []) + AND v.leg = 'W' ), 0) AS wb_bus_l, - COALESCE(SUM(v.volume) FILTER (WHERE - v.movement_uid = 3 AND v.classification_uid = ANY(ARRAY[3]::int []) AND v.leg = 'E' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.movement_uid = 3 + AND v.classification_uid = ANY(ARRAY[3]::int []) + AND v.leg = 'E' ), 0) AS eb_bus_r, - COALESCE(SUM(v.volume) FILTER (WHERE - v.movement_uid = 1 AND v.classification_uid = ANY(ARRAY[3]::int []) AND v.leg = 'E' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.movement_uid = 1 + AND v.classification_uid = ANY(ARRAY[3]::int []) + AND v.leg = 'E' ), 0) AS eb_bus_t, - COALESCE(SUM(v.volume) FILTER (WHERE - v.movement_uid = 2 AND v.classification_uid = ANY(ARRAY[3]::int []) AND v.leg = 'E' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.movement_uid = 2 + AND v.classification_uid = ANY(ARRAY[3]::int []) + AND v.leg = 'E' ), 0) AS eb_bus_l, - COALESCE(SUM(v.volume) FILTER (WHERE - v.classification_uid = 6 - AND v.leg = 'N' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.classification_uid = 6 + AND v.leg = 'N' ), 0) AS nx_peds, - COALESCE(SUM(v.volume) FILTER (WHERE - v.classification_uid = 6 - AND v.leg = 'S' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.classification_uid = 6 + AND v.leg = 'S' ), 0) AS sx_peds, - COALESCE(SUM(v.volume) FILTER (WHERE - v.classification_uid = 6 - AND v.leg = 'E' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.classification_uid = 6 + AND v.leg = 'E' ), 0) AS ex_peds, - COALESCE(SUM(v.volume) FILTER (WHERE - v.classification_uid = 6 - AND v.leg = 'W' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.classification_uid = 6 + AND v.leg = 'W' ), 0) AS wx_peds, - COALESCE(SUM(v.volume) FILTER (WHERE - v.classification_uid = 2 - AND v.leg = 'N' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.classification_uid = 2 + AND v.leg = 'N' ), 0) AS nx_bike, - COALESCE(SUM(v.volume) FILTER (WHERE - v.classification_uid = 2 - AND v.leg = 'S' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.classification_uid = 2 + AND v.leg = 'S' ), 0) AS sx_bike, - COALESCE(SUM(v.volume) FILTER (WHERE - v.classification_uid = 2 - AND v.leg = 'E' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.classification_uid = 2 + AND v.leg = 'E' ), 0) AS ex_bike, - COALESCE(SUM(v.volume) FILTER (WHERE - v.classification_uid = 2 - AND v.leg = 'W' + COALESCE(SUM(v.volume) FILTER ( + WHERE + v.classification_uid = 2 + AND v.leg = 'W' ), 0) AS wx_bike FROM miovision_api.volumes_15min_mvt AS v --JOIN miovision_api.classifications AS c USING (classification_uid) @@ -172,11 +288,14 @@ SELECT * FROM gwolofs.miovision_open_data_wide_15min LIMIT 10000; --noqa: L044 SELECT col_name FROM ( SELECT - 'COALESCE(SUM(v.volume) FILTER (WHERE - v.movement_uid = ' || movement_uid::text || + 'COALESCE(SUM(v.volume) FILTER ( + WHERE + v.movement_uid = ' + || movement_uid::text || ' AND v.classification_uid = ANY( ARRAY[' || string_agg(classification_uid::text, ',') || ']::int [])' || - ' AND v.leg = ''' || leg || ''' + ' + AND v.leg = ''' || leg || ''' ), 0) AS ' || dir || '_' || CASE classification WHEN 'Light' THEN 'car' WHEN 'Bus' THEN 'bus' @@ -214,9 +333,12 @@ UNION ALL SELECT col_name FROM ( SELECT - 'COALESCE(SUM(v.volume) FILTER (WHERE - v.classification_uid = ' || classification_uid::text || - ' AND v.leg = ''' || leg || ''' + 'COALESCE(SUM(v.volume) FILTER ( + WHERE + v.classification_uid = ' + || classification_uid::text || + ' + AND v.leg = ''' || leg || ''' ), 0) AS ' || dir || '_' || CASE classification WHEN 'Bicycle' THEN 'bike'