【发布时间】:2018-02-27 05:18:38
【问题描述】:
我刚刚阅读了这个页面:In SQL, how can you "group by" in ranges?
回复对我有帮助,但我有一个问题,如果范围的 count() 等于 0,则范围不会显示,当 count() 等于 0 时如何显示范围?
这是我的查询:
select
w.Période,
nb from( select t.Période, count(*) as nb
from (select
*,
case
when report.creation_date between '2017-05-28 00:00:00' and '2017-06-25 00:00:00'
then 'P3'
when report.creation_date between '2017-06-25 00:00:00' and '2017-07-23 00:00:00'
then 'P4'
when report.creation_date between '2017-07-23 00:00:00' and '2017-08-20 00:00:00'
then 'P5'
when report.creation_date between '2017-08-20 00:00:00' and '2017-09-17 00:00:00'
then 'P6'
else 'Avant'
end as Période
from report
where report.office_id = 11) as t
group by t.Période ) as w
这是我的结果:
Avant 57
P3 1
P5 2
P6 4
我想要:
Avant 57
P3 1
P4 0
P5 2
P6 4
我尝试过使用这些,但它不起作用。如果 count() 为 null 然后 0 else count() 以 nb 结束,或者当 count() = 0 then 0 else count() 以 nb 结束或 case when count() REGEXP '[^[:digit:]]' then 0 else count() end as nb
【问题讨论】: