случай, когда суммы возвращают нулевые значения - PullRequest
0 голосов
/ 29 апреля 2019

Я написал следующую временную таблицу, чтобы настроить для случая, когда сумма запроса.

Total_count_package_5_15 и total_count_package_5_13 оба возвращают нулевые значения. (Они не должны быть). Данные правильно настроены в предыдущей временной таблице, и я подтвердил, что данные там, как и ожидалось.

create temporary table screening_packages_count_2018 as

select screening_screen_date,
       count(case when screening_package = 1 then 1 end) as count_package_1,
       count(case when screening_package = 2 then 1 end) as count_package_2,
       count(case when screening_package = 3 then 1 end) as count_package_3,
       count(case when screening_package = 4 then 1 end) as count_package_4

from prod.leasing_fact

where date_part(year, screening_screen_date) = 2018

group by screening_screen_date

order by 1;


-- 5 AND 6 PACKAGE TOTALS BASED ON 2018 1-4 COUNTS

    select date_trunc('day', screening_screen_date)                                              as day,
           case
               when (sum(count_package_1) + sum(count_package_2) + sum(count_package_3) <= 75)
                   then (sum(count_package_1) + sum(count_package_2) + sum(count_package_3)) end as total_count_package_5_15,

           case
               when ((sum(count_package_1) + sum(count_package_2) + sum(count_package_3)) >= 76 and
                     (sum(count_package_1) + sum(count_package_2) + sum(count_package_3)) <= 150)
                   then (sum(count_package_1) + sum(count_package_2) + sum(count_package_3)) end as total_count_package_5_13,

           0                                                                                     as total_count_package_6

    from screening_packages_count_2018

    where count_package_4 = 0
    group by day

Я полагаю, что в моем выражении case есть ошибка при использовании сумм, но я не уверен, что здесь происходит. Спасибо!

1 Ответ

1 голос
/ 29 апреля 2019

Попробуйте это ... Поставьте ELSE 0 в первый Select для каждого Count() function. Если есть какие-либо значения NULL, вы не можете SUM NULL значения.

create temporary table screening_packages_count_2018 as

select screening_screen_date,
       count(case when screening_package = 1 then 1 ELSE 0 end) as count_package_1,
       count(case when screening_package = 2 then 1 ELSE 0 end) as count_package_2,
       count(case when screening_package = 3 then 1 ELSE 0 end) as count_package_3,
       count(case when screening_package = 4 then 1 ELSE 0 end) as count_package_4

from prod.leasing_fact

where date_part(year, screening_screen_date) = 2018

group by screening_screen_date

order by 1;


-- 5 AND 6 PACKAGE TOTALS BASED ON 2018 1-4 COUNTS

    select date_trunc('day', screening_screen_date)                                              as day,
           case
               when (sum(count_package_1) + sum(count_package_2) + sum(count_package_3) <= 75)
                   then (sum(count_package_1) + sum(count_package_2) + sum(count_package_3)) end as total_count_package_5_15,

           case
               when ((sum(count_package_1) + sum(count_package_2) + sum(count_package_3)) >= 76 and
                     (sum(count_package_1) + sum(count_package_2) + sum(count_package_3)) <= 150)
                   then (sum(count_package_1) + sum(count_package_2) + sum(count_package_3)) end as total_count_package_5_13,

           0                                                                                     as total_count_package_6

    from screening_packages_count_2018

    where count_package_4 = 0
    group by day
...