我是oracle的新手。我想用30分钟的时间间隔进行用户计数
用户可以登录多个时间段(开始时间:12:30和结束时间:12:45),因此用户将被计入2个时间间隔(12-12:30和12:30- 1:00) 我正在拿12-jul数据
输入
id start_date end_time
30759224 12-JUL-17 12.02.09.430000000 AM 12-JUL-17 12.02.13.310000000 AM
30759224 12-JUL-17 12.02.13.323000000 AM 12-JUL-17 12.07.33.343000000 AM
17244836 12-JUL-17 12.06.19.203000000 AM 12-JUL-17 12.37.49.890000000 AM
30759224 12-JUL-17 12.07.33.357000000 AM 12-JUL-17 12.07.37.813000000 AM
30759224 12-JUL-17 12.07.37.813000000 AM 12-JUL-17 12.07.40.690000000 AM
30759224 12-JUL-17 12.07.40.707000000 AM 12-JUL-17 12.12.58.470000000 AM
30759224 12-JUL-17 12.12.58.487000000 AM 12-JUL-17 12.13.01.937000000 AM
30759224 12-JUL-17 12.13.01.950000000 AM 12-JUL-17 12.18.22.193000000 AM
18623011 12-JUL-17 12.13.20.267000000 AM 12-JUL-17 12.45.38.013000000 AM
30759224 12-JUL-17 11.46.17.827000000 PM 12-JUL-17 11.51.32.267000000 PM
22118088 12-JUL-17 11.47.03.497000000 PM 13-JUL-17 12.20.17.167000000 AM
我需要的输出
count(id) interval
7 1
2 2
. .
. .
. .
2 48
有48个间隔,即24小时...... 30分钟间隔,然后48个间隔
count(id) interval
7 1 (12 - 12:30 am)
2 2 (12:30 - 1:00 am)
. .
. .
. .
2 48(11:30 - 12:00 am)
我只需要一天数据......
答案 0 :(得分:1)
使用数字表(本例中为CTE),然后将其应用于:TheDate作为参数。
with Nums (NN) as
(
select level
from dual
connect by level <=48
)
, Periods as
(
select trunc(:TheDate) + (NN-1)/48 as p_start, trunc(:TheDate) + (NN)/48 as p_end, NN as p_id
from Nums
)
select p.*, count(distinct t1.id) as distinct_users
from Periods p
left join Input t1
on t1.start_date < p_end
and t1.end_date >= p_start
group by p.p_id, p.p_start, p.p_end
您可以交换:sysdate
的日期或单个结果子查询(如果这是您需要的)