From 29755961e6a98628fde9f07d2dc1663248ec8fd6 Mon Sep 17 00:00:00 2001 From: Joaquin Ossa Date: Fri, 21 Feb 2025 19:16:28 +0100 Subject: [PATCH] commit wip --- .../int_resolutions__incidents.sql | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/models/intermediate/resolutions/int_resolutions__incidents.sql b/models/intermediate/resolutions/int_resolutions__incidents.sql index 99912bf..e6d5c62 100644 --- a/models/intermediate/resolutions/int_resolutions__incidents.sql +++ b/models/intermediate/resolutions/int_resolutions__incidents.sql @@ -2,7 +2,7 @@ with stg_resolutions__incidents as ( select * from {{ ref("stg_resolutions__incidents") }} ), - comments_timeline as ( + logs_timeline as ( select i.id_incident, ( @@ -16,8 +16,34 @@ with from jsonb_array_elements(i.comment_logs::jsonb) comment order by (comment ->> 'CreatedDate')::timestamp desc limit 1 - ) as last_comment_date + ) as last_comment_date, + ( + select count(*) from jsonb_array_elements(i.comment_logs::jsonb) comment + ) as comment_count, + ( + select count(*) + from jsonb_array_elements(i.damage_report_items::jsonb) items + ) as number_damaged_items, + from stg_resolutions__incidents i + ), + damage_reported_items as ( + select + i.id_incident, + jsonb_array_elements(i.documents -> 'DamageReport' -> 'Items') as item + from stg_resolutions__incidents i + ) + damage_report_amounts as ( + select + id_incident, + sum((corrective_measures ->> 'Amount')::numeric) as asked_repair_amount, + sum( + (corrective_measures ->> 'SettlementAmount')::numeric + ) as accepted_amount, + from + parsed_data, + jsonb_array_elements(item -> 'CorrectiveMeasures') as corrective_measures + group by id_incident ) select