簡體   English   中英

總和返回空值的情況

[英]case when sums returning null values

我編寫了以下臨時表來設置總和查詢的情況。

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

我相信使用sums時case語句中有錯誤,但是我不確定這是怎么回事。 謝謝!

嘗試此操作...為每個Count() function的第一個Select放置一個ELSE 0 如果有任何NULL值,則不能對SUM NULLSUM 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM