我有以下语句从Teradata DB中的表中提取日期,小时和用户数。 。 。
SELECT
CAST(end_time AS DATE) AS end_date,
EXTRACT(HOUR FROM end_time) AS end_hour,
COUNT(users) AS total_users
FROM table
GROUP BY end_date, end_hour
使用extract()函数时,我的结果集包含缺少的小时数,用户在24小时内没有活动...我想知道是否有任何技术可以解决我的结果集中缺少的小时数?
我无法创建要查阅的查找表,因为我没有必要的权限来在此数据库上创建表。
任何帮助将不胜感激!
答案 0 :(得分:3)
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
0
select 1
1
select 0
union all
select 1
[3888] UNION,INTERSECT或MINUS的SELECT必须引用一个表格。
select 0 from (select 1 as c) t
union all
select 1 from (select 1 as c) t
0
1
或
select 0 from (select 1) t(c)
union all
select 1 from (select 1) t(c)
0
1
答案 1 :(得分:0)
如果您想要数据库中所有日期的所有小时数,那么您可以使用cross join
生成行,然后使用left join
来生成结果:
SELECT d.end_date,
EXTRACT(HOUR FROM end_time) AS end_hour,
COUNT(t.users) AS total_users
FROM (select distinct CAST(end_time AS DATE) AS end_date from table) d CROSS JOIN
(select distinct EXTRACT(HOUR FROM end_time) AS end_hour from table) h LEFT JOIN
table t
ON t.end_date = d.end_date and t.end_hour = d.end_hour
GROUP BY e.end_date, h.end_hour;
如果未表示所有小时数,您可以使用明确的列表:
SELECT d.end_date,
EXTRACT(HOUR FROM end_time) AS end_hour,
COUNT(t.users) AS total_users
FROM (select distinct CAST(end_time AS DATE) AS end_date from table) d CROSS JOIN
(select * from (select 0 as end_hour) t UNION ALL
select * from (select 1 as end_hour) t UNION ALL
. . .
) h LEFT JOIN
table t
ON t.end_date = d.end_date and t.end_hour = d.end_hour
GROUP BY e.end_date, h.end_hour;