简体   繁体   English

DB2 SQL 以 10 分钟为时间间隔的组行计数

[英]DB2 SQL group row count by time intervals of 10 minutes

I have a table in DB2 which contains data such as the following:我在 DB2 中有一个表,其中包含如下数据:

completed_timestamp完成时间戳 details细节
2021-12-19-15.38.10 2021-12-19-15.38.10 abcd A B C D
2021-12-19-15.39.10 2021-12-19-15.39.10 efgh efgh
2021-12-19-15.48.10 2021-12-19-15.48.10 ijkl伊克尔
2021-12-19-15.49.10 2021-12-19-15.49.10 mnop操作
2021-12-19-15.54.10 2021-12-19-15.54.10 qrst第一时间

I want to be able to count the number of rows in the table for every 10 minutes eg我希望能够每 10 分钟计算一次表中的行数,例如

Time时间 count数数
2021-12-19-15.40 2021-12-19-15.40 2 2
2021-12-19-15.50 2021-12-19-15.50 2 2
2021-12-19-16.00 2021-12-19-16.00 1 1

completed_timestamp = Timestamp, details = varchar completed_timestamp = 时间戳,详细信息 = varchar

I've seen this done in other SQL languages but so far have not figured out how to do it with DB2.我已经在其他 SQL 语言中看到了这一点,但到目前为止还没有弄清楚如何使用 DB2 来做到这一点。 How would I do that?我该怎么做?

You can get the minute from the timestamp, divide it by ten, get the next integer, multiply by ten and add it to the hour as minutes:您可以从时间戳中获取分钟,将其除以十,得到下一个 integer,乘以十并将其作为分钟添加到小时:

WITH TABLE1 (completed_timestamp, details) AS (
 VALUES 
 (timestamp '2021-12-19-15.38.10', 'abcd'),
 (timestamp '2021-12-19-15.39.10', 'efgh'),
 (timestamp '2021-12-19-15.48.10', 'ijkl'),
 (timestamp '2021-12-19-15.49.10', 'mnop'),
 (timestamp '2021-12-19-15.54.10', 'qrst')
),
trunc_timestamps (time, details) AS (
  SELECT trunc(completed_timestamp, 'HH24') + (ceiling(minute(completed_timestamp) / 10.0) * 10) MINUTES, details FROM table1
)
SELECT trunc_timestamps.time, count(*) FROM trunc_timestamps GROUP BY trunc_timestamps.time

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

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