commit wip
This commit is contained in:
parent
3f224d8fed
commit
2f484054df
1 changed files with 17 additions and 9 deletions
|
|
@ -41,24 +41,32 @@ with
|
|||
damage_reported_items as (
|
||||
select id_incident, jsonb_array_elements(damage_report_items::jsonb) as item -- No need for 'Items' if already an array
|
||||
from stg_resolutions__incidents
|
||||
where damage_report_items is not null
|
||||
),
|
||||
damage_report as (
|
||||
select
|
||||
id_incident,
|
||||
count(distinct item ->> 'Id') as number_damaged_items,
|
||||
dri.id_incident,
|
||||
count(distinct dri.item ->> 'Id') as number_damaged_items,
|
||||
sum(
|
||||
coalesce((corrective_measures ->> 'OriginalAmount')::numeric, 0)
|
||||
coalesce((cm.corrective_measure ->> 'OriginalAmount')::numeric, 0)
|
||||
) as original_value_amount_in_txn_currency,
|
||||
sum(
|
||||
coalesce((corrective_measures ->> 'Amount')::numeric, 0)
|
||||
coalesce((cm.corrective_measure ->> 'Amount')::numeric, 0)
|
||||
) as asked_repair_amount_in_txn_currency,
|
||||
sum(
|
||||
coalesce((corrective_measures ->> 'SettlementAmount')::numeric, 0)
|
||||
coalesce((cm.corrective_measure ->> 'SettlementAmount')::numeric, 0)
|
||||
) as accepted_amount_in_txn_currency
|
||||
from
|
||||
damage_reported_items,
|
||||
jsonb_array_elements(item -> 'CorrectiveMeasures') as corrective_measures
|
||||
group by id_incident
|
||||
from damage_reported_items dri
|
||||
left join
|
||||
lateral jsonb_array_elements(
|
||||
case
|
||||
when jsonb_typeof(dri.item -> 'CorrectiveMeasures') = 'array'
|
||||
then dri.item -> 'CorrectiveMeasures'
|
||||
else '[]'::jsonb
|
||||
end
|
||||
) as cm(corrective_measure)
|
||||
on true
|
||||
group by dri.id_incident
|
||||
)
|
||||
|
||||
select
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue