简体   繁体   English

Oracle SQL:根据固定时间范围(比如 15 或 30 分钟)计算记录

[英]Oracle SQL: to count the records based on fixed time frame (say 15 or 30 minutes)

I have a table similar to我有一张类似于

Start time            |       End Time                 | User |
09/02/2021 03:01:13   |       09/02/2021 03:45:15      | ABC  |
09/02/2021 03:15:20   |       09/02/2021 05:03:20      | XYZ  |
09/02/2021 06:03:12   |       09/02/2021 06:15:30      | DEF  |

Expecting output:期待 output:

StDt                  |       EndDt                    | Count(1) 
09/02/2021 00:00:00   |       09/02/2021 01:00:00      | 0
09/02/2021 01:00:00   |       09/02/2021 02:00:00      | 0
09/02/2021 02:00:00   |       09/02/2021 03:00:00      | 0
09/02/2021 03:00:00   |       09/02/2021 04:00:00      | 2
09/02/2021 04:00:00   |       09/02/2021 05:00:00      | 1
09/02/2021 05:00:00   |       09/02/2021 06:00:00      | 0
09/02/2021 06:00:00   |       09/02/2021 07:00:00      | 1

The interval in this example is hourly but i would like to keep it flexible for 10 mins/15 mins/30 mins.此示例中的间隔是每小时一次,但我想保持灵活 10 分钟/15 分钟/30 分钟。 I want this to be written in single sql.我希望它用单个 sql 编写。 All i could work out till now is how to generate the range.到目前为止,我所能解决的只是如何生成范围。

select t1.StartDt, t1.EndDt from 
(
  select 
    (to_char(timestamp '2021-02-09 00:00:00' + numtodsinterval(rownum*60,'MINUTE') - numtodsinterval(60,'MINUTE'),'DD-MM-YYYY hh24:mi')) as StartDt,
    (to_char(timestamp '2021-02-09 00:00:00' + numtodsinterval(rownum*60,'MINUTE'),'DD-MM-YYYY hh24:mi')) as EndDt
  from dual connect by level <= 24
) t1;

I dont know how to link to the table mentioned above to get the data in the format i require.我不知道如何链接到上面提到的表格以获取我需要的格式的数据。

You have such a nice startup, except keep the timestamp format for the time values within the subquery, and move TO_CHAR formatting to the main query at the result displaying phase along with using correlated subquery with distinctly count aggregation for the overlapping intervals, and use bind variables as the placeholder for the time portion values( 60 , 30 , 15 ) such as你有一个很好的启动,除了保留子查询中时间值的时间戳格式,并在结果显示阶段将TO_CHAR格式移动到主查询,同时使用相关子查询和重叠间隔的明显计数聚合,并使用绑定变量作为时间部分值( 60 )的30 15 ,例如

SQL> var min number
SQL> exec :min := 60
 
PL/SQL procedure successfully completed
min
---------
60
 
SQL> SELECT TO_CHAR(t.StartDt,'DD-MM-YYYY HH24:MI') AS StartDt,
  2         TO_CHAR(t.EndDt,'DD-MM-YYYY HH24:MI') AS EndDt,
  3         ( SELECT COUNT(DISTINCT "User") 
                FROM tab 
               WHERE t.EndDt >= Start_Time 
                 AND t.StartDt <= End_Time ) AS Count
  4    FROM
  5    (
  6       SELECT timestamp '2021-02-09 00:00:00' +
  7                        numtodsinterval(rownum * :min, 'MINUTE') -
  8                        numtodsinterval(:min, 'MINUTE') AS StartDt,
  9              timestamp '2021-02-09 00:00:00' +
 10                        numtodsinterval(rownum * :min, 'MINUTE') AS EndDt
 11         FROM dual
 12      CONNECT BY level <= 24
 13    ) t
 14   ORDER BY StartDt;
 
STARTDT          ENDDT                 COUNT
---------------- ---------------- ----------
09-02-2021 00:00 09-02-2021 01:00          0
09-02-2021 01:00 09-02-2021 02:00          0
09-02-2021 02:00 09-02-2021 03:00          0
09-02-2021 03:00 09-02-2021 04:00          2
09-02-2021 04:00 09-02-2021 05:00          1
09-02-2021 05:00 09-02-2021 06:00          1
09-02-2021 06:00 09-02-2021 07:00          1
09-02-2021 07:00 09-02-2021 08:00          0
.....
.....

Demo 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM