我写了下面的临时表来设置总和查询的情况。
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语句中有错误,但是我不确定这是怎么回事。谢谢!
答案 0 :(得分:1)
尝试此操作...将ELSE 0
的{{1}}放在第一个Select
中。如果有任何Count() function
值,则不能为NULL
值。
SUM NULL