DB2 SQL组行计数(按10分钟的时间间隔)

zvms9eto  于 2022-11-07  发布在  DB2
关注(0)|答案(1)|浏览(331)

我在DB2中有一个表,其中包含如下数据:
| 已完成时间戳|详细信息|
| - -|- -|
| 2021-12-19-15.38.10 |abcd语言|
| 2021-12-19-15.39.10 |埃夫格|
| 2021-12-19-15.48.10 |伊克尔|
| 2021-12-19-15.49.10 |主操作程序|
| 2021-12-19-15.54.10 |第一|
我希望能够每10分钟计算一次表中的行数,例如:
| 时间|计数器|
| - -|- -|
| 2021-12-19-15.40 |2个|
| 2021-12-19-15.50 |2个|
| 2021-12-19-16.00 |一个|
completed_timestamp =时间戳,详细信息= varchar
我在其他SQL语言中见过这样做,但到目前为止还不知道如何在DB2中做到这一点。

4uqofj5v

4uqofj5v1#

您可以从时间戳中获取分钟,将其除以10,得到下一个整数,乘以10,然后将其作为分钟添加到小时:

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

相关问题