我有下面的 PLSQL 查询,根据时间戳的输入范围返回一些时间戳列表。
select data_timestamp
from table1
where data_timestamp > :input_start_ts
and data_timestamp < :input_end_ts;
**输出:**
2024-07-10 10:21:10
2024-07-10 10:21:18
2024-07-10 10:21:21
2024-07-10 10:21:22
2024-07-10 10:21:23
2024-07-10 10:21:25
2024-07-10 10:21:26
2024-07-10 10:21:29
2024-07-10 10:21:30
2024-07-10 10:21:31
2024-07-10 10:21:32
2024-07-10 10:21:40
2024-07-10 10:21:49
2024-07-10 10:21:55
2024-07-10 10:21:56
2024-07-10 10:22:01
2024-07-10 10:22:40
我正在尝试将这些时间戳分组为 20 秒窗口,并为该组指示器添加新列:
预期输出:
2024-07-10 10:21:10 1
2024-07-10 10:21:18 1
2024-07-10 10:21:21 1
2024-07-10 10:21:22 1
2024-07-10 10:21:23 1
2024-07-10 10:21:25 1
2024-07-10 10:21:26 1
2024-07-10 10:21:29 1
2024-07-10 10:21:30 2
2024-07-10 10:21:31 2
2024-07-10 10:21:32 2
2024-07-10 10:21:40 2
2024-07-10 10:21:49 2
2024-07-10 10:21:55 3
2024-07-10 10:21:56 3
2024-07-10 10:22:01 3
2024-07-10 10:22:40 4
你能引导我走向正确的方向吗
您的问题不完全清楚,“20秒窗口”是什么意思。这个窗口应该从哪里开始?总是在第一排?
起点是这样的:
WITH t AS (
SELECT data_timestamp,
TRUNC(data_timestamp, 'mi') + TRUNC(EXTRACT(SECOND FROM data_timestamp) / 20) * INTERVAL '20' SECOND AS data_interval
FROM table1)
SELECT data_timestamp, data_interval,
DENSE_RANK() OVER (ORDER BY data_interval)
FROM t;
我想你需要添加一些偏移量,就是这个:
WITH t AS (
SELECT data_timestamp,
TRUNC(data_timestamp, 'mi') + TRUNC(EXTRACT(SECOND FROM data_timestamp) / 20) * INTERVAL '20' SECOND
+ EXTRACT(SECOND FROM MIN(chng_date_implemented) OVER ()) * INTERVAL '1' SECOND AS data_interval
FROM table1)
SELECT data_timestamp, data_interval,
DENSE_RANK() OVER (ORDER BY data_interval)
FROM t;
注意,上述查询适用于数据类型
TIMESTAMP
。如果您的表使用数据类型 DATE
那么您必须使用 CAST(data_timestamp AS TIMESTAMP)