oracle 以15分钟为增量进行分组,包括计数0

ecfsfe2w  于 2023-03-29  发布在  Oracle
关注(0)|答案(1)|浏览(206)

我有下面的查询,它以15分钟的增量对我的数据进行分组,但它不包括在15分钟增量中没有数据的增量。
当前查询:

SELECT 
    TO_CHAR(TRUNC(time_stamp)
        + FLOOR(TO_NUMBER(TO_CHAR(time_stamp, 'SSSSS'))/900)/96, 'YYYY-MM-DD HH24:MI:SS') time_start,
    COUNT (CUSTOMERS) Customer_Calls
FROM CUSTOMERS
WHERE time_stamp >= to_date('2023-03-23 00:00:00', 'YYYY-MM-DD HH24:MI:SS')
GROUP BY
    TRUNC(time_stamp) + FLOOR(TO_NUMBER(TO_CHAR(time_stamp, 'SSSSS'))/900)/96;

电流输出:

2023-03-23 00:30:00 1
2023-03-23 00:45:00 1
2023-03-23 01:45:00 1
2023-03-23 03:45:00 1

我希望得到每15分钟的增量。举个例子:
请求输出

2023-03-23 00:00:00 0
2023-03-23 00:15:00 0
2023-03-23 00:30:00 1
2023-03-23 00:45:00 1
2023-03-23 01:00:00 0
2023-03-23 01:15:00 0
2023-03-23 01:30:00 0
2023-03-23 01:45:00 1
and so on.

任何帮助都将不胜感激。谢谢!

xfyts7mz

xfyts7mz1#

不是所有的时隙都在表中可用,所以我们需要首先生成它们;为此,我们可以使用递归。然后,我们用一个left join引入customers表。
假设您想要从昨天午夜到现在的所有时隙:

with slots (ts) as (
    select trunc(sysdate) - interval '1' day ts from dual
    union all
    select ts + interval '15' minute from slots where ts < sysdate
)
select s.ts, count(c.time_stamp) as customer_call
from slots
left join customers c 
    on c.time_stamp  >= s.ts
    and c.time_stamp <  s.ts + interval '15' minute
group by s.ts

相关问题