Skip to content

Commit

Permalink
Resolve kpi_def_latest view with circle check (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aurelieph authored Oct 21, 2023
1 parent ac813bd commit bbb7de7
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions migrations/V22__fix_circle_check_on_kpi_def_view.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
CREATE OR REPLACE VIEW public.kpi_definition_with_latest_values AS
select
ckd.circle_kpidef_id,
ckd.circle_id,
kd.kpi_id,
kd.kpi_name,
kd.description,
kd.periodicity,
kd.value_min,
kd.value_max,
kd.unit,
kd.created_at as kpi_created_at,
kd.updated_at as kpi_updated_at,
kd.active,
kd.is_approved,
t1.latest_value,
t1.latest_user_id,
t1.latest_standardized_date,
c.circle_name,
t.target_id,
t.target_value
from
circle_kpi_definition ckd
join kpi_definition kd on ckd.kpi_id = kd.kpi_id
left join (
select
kv.kpi_id,
kv.circle_id,
kv.standardized_date as latest_standardized_date,
kv.value as latest_value,
kv.user_id as latest_user_id
from
kpi_values_period_standardized kv
join (
select
kpi_values_period_standardized.kpi_id,
kpi_values_period_standardized.circle_id,
max(kpi_values_period_standardized.standardized_date) as max_standardized_date
from
kpi_values_period_standardized
group by
kpi_values_period_standardized.kpi_id,
kpi_values_period_standardized.circle_id
) max_dates on kv.kpi_id = max_dates.kpi_id
and kv.circle_id = max_dates.circle_id
and kv.standardized_date = max_dates.max_standardized_date
) t1 on ckd.kpi_id = t1.kpi_id and ckd.circle_id= t1.circle_id
left join circle c on t1.circle_id = c.circle_id
left join target t on kd.kpi_id = t.kpi_id and ckd.circle_id = t.circle_id
order by 1;

0 comments on commit bbb7de7

Please sign in to comment.