想使用 sql 统计开始时间和结束时间的在线人数
want to use sql count online people with begintime and endtime
我想使用 sql 快速计算开始时间和结束时间的在线人数。
我的数据喜欢:
userid begintime endtime
023150000030040 2020-03-05 12:50:46 2020-03-05 12:50:49
023150004186637 2020-03-05 10:31:19 2020-03-05 10:31:24
023150000788581 2020-03-05 00:59:01 2020-03-05 01:02:00
023150004411606 2020-03-05 19:55:42 2020-03-05 20:02:51
023150004066308 2020-03-05 18:48:03 2020-03-05 18:58:03
023150002033547 2020-03-05 12:39:24 2020-03-05 12:42:21
023150000030040 2020-03-05 13:26:02 2020-03-05 13:26:04
023150003690798 2020-03-05 02:04:50 2020-03-05 02:14:50
023150000030040 2020-03-05 13:57:10 2020-03-05 13:57:12
023150004460558 2020-03-05 16:44:48 2020-03-05 16:47:58
我想统计每小时在线人数。现在我有一个愚蠢的方法来计算。我的 sql 赞:
select '01' as hour,COUNT(distinct T.userid)
from datamart_ott_b2b_jsydcp.f_tplay t where t.topicdate ='2020-03-05'
and t.begintime < date_parse('2020-03-05 01', '%Y-%m-%d %h')
and t.endtime > date_parse('2020-03-05 00', '%Y-%m-%d %h')
union all
select '02' as hour,COUNT(distinct T.userid)
from datamart_ott_b2b_jsydcp.f_tplay t where t.topicdate ='2020-03-05'
and t.begintime < date_parse('2020-03-05 02', '%Y-%m-%d %h')
and t.endtime > date_parse('2020-03-05 01', '%Y-%m-%d %h')
.......
有更简单的方法吗?谢谢
使用日历 table 方法,我们可以在小时和日期匹配的情况下将包含所有 24 小时的 table 左连接到您当前的 table。然后,我们可以按小时聚合并计算不同的用户以生成您想要的输出。
WITH hours AS (
SELECT 0 AS hour UNION ALL
SELECT 1 UNION ALL
SELECT 2 UNION ALL
...
SELECT 23
)
SELECT
h.hour,
COUNT(DISTINCT t.userid) AS user_cnt
FROM hours h
LEFT JOIN datamart_ott_b2b_jsydcp.f_tplay t
ON h.hour = DATE_TRUNC('hour', t.topicdate) AND
t.topicdate = '2020-03-05'
GROUP BY
h.hour
ORDER BY
h.hour;
在 Prestodb 中,您可以生成一个包含整数值的数组,然后取消嵌套它们以获取小时数。然后使用 join
s 和 group by
进行您想要的计算:
select hh.hh as hour, cont(distinct t.userid)
from (select sequence(0, 23) hhs
) h cross join
unnest(h.hhs) as hh(hh) left join
datamart_ott_b2b_jsydcp.f_tplay t
on hour(begintime) <= hh.hh and
hour(enddtime) >= hh.hh
where t.topicdate = '2020-03-05'
group by hh.hh
order by hh.hh;
我想使用 sql 快速计算开始时间和结束时间的在线人数。
我的数据喜欢:
userid begintime endtime
023150000030040 2020-03-05 12:50:46 2020-03-05 12:50:49
023150004186637 2020-03-05 10:31:19 2020-03-05 10:31:24
023150000788581 2020-03-05 00:59:01 2020-03-05 01:02:00
023150004411606 2020-03-05 19:55:42 2020-03-05 20:02:51
023150004066308 2020-03-05 18:48:03 2020-03-05 18:58:03
023150002033547 2020-03-05 12:39:24 2020-03-05 12:42:21
023150000030040 2020-03-05 13:26:02 2020-03-05 13:26:04
023150003690798 2020-03-05 02:04:50 2020-03-05 02:14:50
023150000030040 2020-03-05 13:57:10 2020-03-05 13:57:12
023150004460558 2020-03-05 16:44:48 2020-03-05 16:47:58
我想统计每小时在线人数。现在我有一个愚蠢的方法来计算。我的 sql 赞:
select '01' as hour,COUNT(distinct T.userid)
from datamart_ott_b2b_jsydcp.f_tplay t where t.topicdate ='2020-03-05'
and t.begintime < date_parse('2020-03-05 01', '%Y-%m-%d %h')
and t.endtime > date_parse('2020-03-05 00', '%Y-%m-%d %h')
union all
select '02' as hour,COUNT(distinct T.userid)
from datamart_ott_b2b_jsydcp.f_tplay t where t.topicdate ='2020-03-05'
and t.begintime < date_parse('2020-03-05 02', '%Y-%m-%d %h')
and t.endtime > date_parse('2020-03-05 01', '%Y-%m-%d %h')
.......
有更简单的方法吗?谢谢
使用日历 table 方法,我们可以在小时和日期匹配的情况下将包含所有 24 小时的 table 左连接到您当前的 table。然后,我们可以按小时聚合并计算不同的用户以生成您想要的输出。
WITH hours AS (
SELECT 0 AS hour UNION ALL
SELECT 1 UNION ALL
SELECT 2 UNION ALL
...
SELECT 23
)
SELECT
h.hour,
COUNT(DISTINCT t.userid) AS user_cnt
FROM hours h
LEFT JOIN datamart_ott_b2b_jsydcp.f_tplay t
ON h.hour = DATE_TRUNC('hour', t.topicdate) AND
t.topicdate = '2020-03-05'
GROUP BY
h.hour
ORDER BY
h.hour;
在 Prestodb 中,您可以生成一个包含整数值的数组,然后取消嵌套它们以获取小时数。然后使用 join
s 和 group by
进行您想要的计算:
select hh.hh as hour, cont(distinct t.userid)
from (select sequence(0, 23) hhs
) h cross join
unnest(h.hhs) as hh(hh) left join
datamart_ott_b2b_jsydcp.f_tplay t
on hour(begintime) <= hh.hh and
hour(enddtime) >= hh.hh
where t.topicdate = '2020-03-05'
group by hh.hh
order by hh.hh;