From ee8f3b2c8bb40d29aa5ea3819efa1ae88c600490 Mon Sep 17 00:00:00 2001 From: gabrielwol <80077912+gabrielwol@users.noreply.github.com> Date: Fri, 26 Jul 2024 20:52:27 +0000 Subject: [PATCH] #868, #879 update padding trigger to return rows inserted --- ...d_intersection_movement_padding_values.sql | 36 +++++++++++-------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/volumes/miovision/sql/function/function-add_intersection_movement_padding_values.sql b/volumes/miovision/sql/function/function-add_intersection_movement_padding_values.sql index 68b2aaf21..1d6e3cec4 100644 --- a/volumes/miovision/sql/function/function-add_intersection_movement_padding_values.sql +++ b/volumes/miovision/sql/function/function-add_intersection_movement_padding_values.sql @@ -6,6 +6,8 @@ COST 100 VOLATILE SECURITY DEFINER PARALLEL UNSAFE AS $BODY$ +DECLARE n_inserted numeric; + BEGIN WITH temp AS ( @@ -86,23 +88,29 @@ aggregate_insert AS ( --select real value instead of padding value if available v.volume DESC RETURNING intersection_uid, volume_15min_mvt_uid, datetime_bin, classification_uid, leg, movement_uid, volume +), + +updated AS ( + --To update foreign key for 1min bin table + UPDATE miovision_api.volumes AS v + SET volume_15min_mvt_uid = a_i.volume_15min_mvt_uid + FROM aggregate_insert AS a_i + WHERE + v.volume_15min_mvt_uid IS NULL + AND a_i.volume > 0 + AND v.intersection_uid = a_i.intersection_uid + AND v.datetime_bin >= a_i.datetime_bin + AND v.datetime_bin < a_i.datetime_bin + interval '15 minutes' + AND v.classification_uid = a_i.classification_uid + AND v.leg = a_i.leg + AND v.movement_uid = a_i.movement_uid ) ---To update foreign key for 1min bin table -UPDATE miovision_api.volumes AS v -SET volume_15min_mvt_uid = a_i.volume_15min_mvt_uid -FROM aggregate_insert AS a_i -WHERE - v.volume_15min_mvt_uid IS NULL - AND a_i.volume > 0 - AND v.intersection_uid = a_i.intersection_uid - AND v.datetime_bin >= a_i.datetime_bin - AND v.datetime_bin < a_i.datetime_bin + interval '15 minutes' - AND v.classification_uid = a_i.classification_uid - AND v.leg = a_i.leg - AND v.movement_uid = a_i.movement_uid; +SELECT COUNT(*) INTO n_inserted +FROM aggregate_insert; -RAISE NOTICE '% Done adding to 15min MVT bin based on intersection_movement new rows.', timeofday(); +RAISE NOTICE '% Done adding to 15min MVT bin based on intersection_movement new rows. % rows added.', timeofday(), n_inserted; +RETURN NULL; END; $BODY$;