- sys_calendar.calendar產生所要求的日期(根據需要更改範圍)
- WITH RECURSIVE以生成小時
with recursive cte_hours (hr)
as
(
select 0 from (select 1) t(c)
union all select hr + 1 from cte_hours where hr < 23
)
select c.calendar_date as dt
,h.hr as hr
,zeroifnull(t.total_users) as total_users
from sys_calendar.calendar as c
cross join cte_hours as h
left join (select cast(end_time as date) as end_date
,extract(hour from end_time) as end_hour
,count(users) as total_users
from mytable t
group by end_date
,end_hour
) t
on t.end_date = c.calendar_date
and t.end_hour = h.hr
where c.calendar_date between current_date - 10 and current_date
order by dt,hr
;
對於@GordonLinoff
select 0
select 1
select 0
union all
select 1
[3888]一種選擇用於UNION,相交或MINUS必須引用的表。
select 0 from (select 1 as c) t
union all
select 1 from (select 1 as c) t
或
select 0 from (select 1) t(c)
union all
select 1 from (select 1) t(c)
失蹤的日子呢? –
如果你不能創建一個表,你可以創建一個臨時表:'CREATE VOLATILE TABLE hours(hh BYTEINT UNIQUE NOT NULL)ON COMMITE PRESERVE ROWS',然後插入24行。 – dnoeth