SQL Oracle - 按 ID、任务 ID、最小和最大时间戳分组
Posted
技术标签:
【中文标题】SQL Oracle - 按 ID、任务 ID、最小和最大时间戳分组【英文标题】:SQL Oracle - Group by ID, task ID, min and max timestamp 【发布时间】:2021-06-16 11:28:20 【问题描述】:我有用户执行不同任务的数据。 我想根据用户 ID 和任务 ID 对这些数据进行分组,以获取每个任务的开始和结束时间。当员工切换到另一个任务时,应该有一个新的行,其中包含新的开始和结束时间。
简化数据集示例:
userid | taskid | date_time_stamp (ascending) |
---|---|---|
1 | task-A | 16/6/2021 04:17:00 |
1 | task-A | 16/6/2021 04:19:00 |
1 | task-A | 16/6/2021 04:27:00 |
1 | task-B | 16/6/2021 04:31:00 |
1 | task-B | 16/6/2021 04:33:00 |
1 | task-B | 16/6/2021 04:36:00 |
1 | task-A | 16/6/2021 04:42:00 |
1 | task-A | 16/6/2021 04:44:00 |
示例结果
userid | taskid | first_dtm | last_dtm |
---|---|---|---|
1 | task-A | 16/6/2021 04:17:00 | 16/6/2021 04:27:00 |
1 | task-B | 16/6/2021 04:31:00 | 16/6/2021 04:36:00 |
1 | task-A | 16/6/2021 04:42:00 | 16/6/2021 04:44:00 |
我知道我应该使用一些结合 GROUP BY 的 min() 和 max() 函数。但是,在此示例中,按 userid 和 taskid 分组将导致 task-A 仅占一行。
【问题讨论】:
这是常见的“差距和岛屿”任务。 PS。 OracleDB 或 mysql,而不是两者都选 - 选择一个并编辑标签列表。 【参考方案1】:从 Oracle 12 开始,您可以使用MATCH_RECOGNIZE
:
SELECT *
FROM table_name
MATCH_RECOGNIZE(
PARTITION BY userid
ORDER BY date_time_stamp
MEASURES
FIRST(taskid) AS taskid,
FIRST(date_time_stamp) AS start_date,
LAST(date_time_stamp) AS end_date
ONE ROW PER MATCH
PATTERN ( same_task+ )
DEFINE same_task AS FIRST(taskid) = taskid
)
在此之前,可以使用ROW_NUMBER
解析函数并聚合:
SELECT userid,
taskid,
MIN(date_time_stamp) AS start_date,
MAX(date_time_stamp) AS end_date
FROM (
SELECT t.*,
ROW_NUMBER() OVER ( PARTITION BY userid ORDER BY date_time_stamp )
- ROW_NUMBER() OVER ( PARTITION BY userid, taskid ORDER BY date_time_stamp )
AS grp
FROM table_name t
)
GROUP BY userid, taskid, grp
ORDER BY userid, start_date
其中,对于您的示例数据:
CREATE TABLE table_name ( userid, taskid, date_time_stamp ) AS
SELECT 1, 'task-A', DATE '2021-06-16' + INTERVAL '04:17:00' HOUR TO SECOND FROM DUAL UNION ALL
SELECT 1, 'task-A', DATE '2021-06-16' + INTERVAL '04:19:00' HOUR TO SECOND FROM DUAL UNION ALL
SELECT 1, 'task-A', DATE '2021-06-16' + INTERVAL '04:27:00' HOUR TO SECOND FROM DUAL UNION ALL
SELECT 1, 'task-B', DATE '2021-06-16' + INTERVAL '04:31:00' HOUR TO SECOND FROM DUAL UNION ALL
SELECT 1, 'task-B', DATE '2021-06-16' + INTERVAL '04:33:00' HOUR TO SECOND FROM DUAL UNION ALL
SELECT 1, 'task-B', DATE '2021-06-16' + INTERVAL '04:36:00' HOUR TO SECOND FROM DUAL UNION ALL
SELECT 1, 'task-A', DATE '2021-06-16' + INTERVAL '04:42:00' HOUR TO SECOND FROM DUAL UNION ALL
SELECT 1, 'task-A', DATE '2021-06-16' + INTERVAL '04:44:00' HOUR TO SECOND FROM DUAL
两个输出:
USERID TASKID START_DATE END_DATE 1 task-A 2021-06-16 04:17:00 2021-06-16 04:27:00 1 task-B 2021-06-16 04:31:00 2021-06-16 04:36:00 1 task-A 2021-06-16 04:42:00 2021-06-16 04:44:00
db小提琴here
【讨论】:
以上是关于SQL Oracle - 按 ID、任务 ID、最小和最大时间戳分组的主要内容,如果未能解决你的问题,请参考以下文章