From 4e5f646f2a45c3301c31331905492860f82ea867 Mon Sep 17 00:00:00 2001 From: Jasmin Sert <63225759+sisiphos3@users.noreply.github.com> Date: Thu, 26 Oct 2023 12:12:52 +0200 Subject: [PATCH] add changes to view period_standardised (#42) * add changes to view period_standardised * remove V20 * change branch in yml file * test commit * change drop in V27 * add view name * replace ' * change branch in yml --- ...V20__update_logs_view_with_periodicity.sql | 41 ------------- ...view_period_standardised_groupedbyyear.sql | 61 +++++++++++++++++++ 2 files changed, 61 insertions(+), 41 deletions(-) delete mode 100644 migrations/V20__update_logs_view_with_periodicity.sql create mode 100644 migrations/V27__view_period_standardised_groupedbyyear.sql diff --git a/migrations/V20__update_logs_view_with_periodicity.sql b/migrations/V20__update_logs_view_with_periodicity.sql deleted file mode 100644 index 89209ed..0000000 --- a/migrations/V20__update_logs_view_with_periodicity.sql +++ /dev/null @@ -1,41 +0,0 @@ -create or replace view - public.kpi_logs as -select - kvh.kpi_value_history_id, - kvh.kpi_id, - kvh.circle_id, - kvh.user_id, - kvh.value, - kvh.period_date, - kvh.action, - kvh.created_at, - ku.user_name, - uwdc.circle_id as user_circle_id, - c.circle_name as user_circle_name, - case - when kvh.periodicity = 'yearly'::periodicity then date_trunc( - 'year'::text, - kvh.period_date::timestamp with time zone - ) + '1 year'::interval - '1 day'::interval - when kvh.periodicity = 'monthly'::periodicity then date_trunc( - 'month'::text, - kvh.period_date::timestamp with time zone - ) + '1 mon'::interval - '1 day'::interval - when kvh.periodicity = 'quarterly'::periodicity then date_trunc( - 'quarter'::text, - kvh.period_date::timestamp with time zone - ) + '3 mons'::interval - '1 day'::interval - when kvh.periodicity = 'weekly'::periodicity then date_trunc( - 'week'::text, - kvh.period_date::timestamp with time zone - ) - '1 day'::interval + '7 days'::interval - else kvh.period_date::timestamp with time zone - end as historical_standardized_date, - kvh.periodicity -from - kpi_values_history kvh - left join kpi_user ku on kvh.user_id = ku.user_id - left join username_with_default_circle uwdc on kvh.user_id = uwdc.user_id - left join circle c on uwdc.circle_id = c.circle_id -order by - kvh.created_at desc; \ No newline at end of file diff --git a/migrations/V27__view_period_standardised_groupedbyyear.sql b/migrations/V27__view_period_standardised_groupedbyyear.sql new file mode 100644 index 0000000..fcadb59 --- /dev/null +++ b/migrations/V27__view_period_standardised_groupedbyyear.sql @@ -0,0 +1,61 @@ +CREATE or REPLACE VIEW public.kpi_values_period_standardized as +select *, +Case when target_value > 0 then ROUND(cumulative_value/target_value*100, 0) else null end as target_fulfilled +FROM +( +select distinct + on ( + kd.kpi_id, + standardized_date, + t1.circle_id + ) t1.kpi_value_history_id, + t1.kpi_id, + t1.circle_id, + t1.user_id, + t1.value, + t1.period_date, + t1.action, + t1.created_at, + kd.periodicity as kpi_periodicity, + case + when kd.periodicity = 'yearly'::periodicity then date_trunc( + 'year'::text, + t1.period_date::timestamp with time zone + ) + '1 year'::interval - '1 day'::interval + when kd.periodicity = 'monthly'::periodicity then date_trunc( + 'month'::text, + t1.period_date::timestamp with time zone + ) + '1 mon'::interval - '1 day'::interval + when kd.periodicity = 'quarterly'::periodicity then date_trunc( + 'quarter'::text, + t1.period_date::timestamp with time zone + ) + '3 mons'::interval - '1 day'::interval + when kd.periodicity = 'weekly'::periodicity then date_trunc( + 'week'::text, + t1.period_date::timestamp with time zone + ) - '1 day'::interval + '7 days'::interval + else t1.period_date::timestamp with time zone + end as standardized_date, + ku.user_name, + t1.comment, + case when kd.cumulative = false and kd.formula = 'aggregate' then sum(t1.value) over (partition by t1.kpi_id, t1.circle_id, EXTRACT(YEAR FROM t1.period_date) order by t1.period_date) + when kd.cumulative = true and kd.formula = 'aggregate' then t1.value + when kd.cumulative = false and kd.formula = 'average' then avg(t1.value) over (partition by t1.kpi_id, t1.circle_id, EXTRACT(YEAR FROM t1.period_date) order by t1.period_date) + when kd.cumulative = true and kd.formula = 'average' then t1.value + end as cumulative_value, + case when kd.formula = 'aggregate' then 'area_graph' + when kd.formula = 'average' and kd.unit in ('numeric', '%') then 'line_graph' + when kd.formula = 'average' and kd.unit = 'boolean' then 'donut_graph' + end as graph_type, + tar.target_value +from + kpi_values_history t1 + join kpi_definition kd on t1.kpi_id = kd.kpi_id + left join kpi_user ku on t1.user_id = ku.user_id + left join target tar on tar.kpi_id = t1.kpi_id AND tar.circle_id = t1.circle_id +order by + kd.kpi_id, + standardized_date desc, + t1.circle_id, + t1.created_at desc +) as sq; \ No newline at end of file