I've written the following temp table to set up for a case when sum query.
Total_count_package_5_15 and total_count_package_5_13 are both returning null values. (They shouldn't be). The data is properly set up in the preceding temporary table and I've confirmed that the data is there as expected.
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
I believe that there is an error in my case statement when using sums, but I'm not sure what is going on here. Thanks!
Try this... Put an ELSE 0
in the first Select
for each Count() function
. If there are any NULL
values, you can't SUM NULL
values.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.