Загрузка данных


with r as (
    select
        rd.*,
        (
            sum_dt_pu is not null
            or sum_ct_pu is not null
        ) as has_pu,
        (
            sum_dt_bu is not null
            or sum_ct_bu is not null
        ) as has_bu
    from back_office.report_conformity_pu_bu_detail rd
    where report_conformity_pu_bu_id = 94
)
select
    count(*) as total_rows,
    count(*) filter (where has_pu) as pu_rows,
    count(*) filter (where has_bu) as bu_rows,
    count(*) filter (
        where has_pu and has_bu
    ) as matched_rows,
    count(*) filter (
        where not has_pu and not has_bu
    ) as empty_rows,
    sum(sum_dt_pu) as total_dt_pu,
    sum(sum_ct_pu) as total_ct_pu,
    sum(sum_dt_bu) as total_dt_bu,
    sum(sum_ct_bu) as total_ct_bu
from r;