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 (
|
damage_reported_items as (
|
||||||
select id_incident, jsonb_array_elements(damage_report_items::jsonb) as item -- No need for 'Items' if already an array
|
select id_incident, jsonb_array_elements(damage_report_items::jsonb) as item -- No need for 'Items' if already an array
|
||||||
from stg_resolutions__incidents
|
from stg_resolutions__incidents
|
||||||
|
where damage_report_items is not null
|
||||||
),
|
),
|
||||||
damage_report as (
|
damage_report as (
|
||||||
select
|
select
|
||||||
id_incident,
|
dri.id_incident,
|
||||||
count(distinct item ->> 'Id') as number_damaged_items,
|
count(distinct dri.item ->> 'Id') as number_damaged_items,
|
||||||
sum(
|
sum(
|
||||||
coalesce((corrective_measures ->> 'OriginalAmount')::numeric, 0)
|
coalesce((cm.corrective_measure ->> 'OriginalAmount')::numeric, 0)
|
||||||
) as original_value_amount_in_txn_currency,
|
) as original_value_amount_in_txn_currency,
|
||||||
sum(
|
sum(
|
||||||
coalesce((corrective_measures ->> 'Amount')::numeric, 0)
|
coalesce((cm.corrective_measure ->> 'Amount')::numeric, 0)
|
||||||
) as asked_repair_amount_in_txn_currency,
|
) as asked_repair_amount_in_txn_currency,
|
||||||
sum(
|
sum(
|
||||||
coalesce((corrective_measures ->> 'SettlementAmount')::numeric, 0)
|
coalesce((cm.corrective_measure ->> 'SettlementAmount')::numeric, 0)
|
||||||
) as accepted_amount_in_txn_currency
|
) as accepted_amount_in_txn_currency
|
||||||
from
|
from damage_reported_items dri
|
||||||
damage_reported_items,
|
left join
|
||||||
jsonb_array_elements(item -> 'CorrectiveMeasures') as corrective_measures
|
lateral jsonb_array_elements(
|
||||||
group by id_incident
|
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
|
select
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue