Summing range of times without counting overlaps twice(不计算重叠的时间范围相加两次)
问题描述
对于给定的用户 ID1"和给定的日期 2018-01-02,我想计算记录的总小时数,其中可能存在重叠.
For a given user ID "1" and a given day 2018-01-02, I want to calculate the total amount of hours logged, where overlaps can exist.
计算这个子集:
+-----+---------------------+---------------------+
| uid | time_start | time_end |
+-----+---------------------+---------------------+
| 1 | 2018-01-02 04:00:00 | 2018-01-02 04:30:00 |
| 1 | 2018-01-02 04:25:00 | 2018-01-02 04:35:00 |
| 1 | 2018-01-02 04:55:00 | 2018-01-02 05:15:00 |
+-----+---------------------+---------------------+
结果时间应该是:00:55.
推荐答案
MariaDB 10.3 具有窗口函数和 CTE,因此您可以使用它们来生成结果.CTE 通过将当前 time_start
与当天的最大先前 time_end
进行比较并取它们的最大(最大)值,然后查询只需 SUM
s 每个会话时间,按用户 ID 和日期分组.请注意,如果一个会话与另一个会话完全重叠,CTE 会将 start
和 end
时间都设置为重叠会话的 end
时间,从而导致有效会话长度为 0.我扩展了我的演示以包含这样的场景,以及多个重叠会话:
MariaDB 10.3 has window functions and CTE's so you can use those to generate your results. The CTE removes the overlaps from the session times by comparing the current time_start
with the maximum previous time_end
for that day and taking the maximum (greatest) value of them and then the query simply SUM
s each session time, grouping by user id and date. Note that if one session is completely overlapped by another, the CTE sets both start
and end
times to the end
time of the overlapping session, resulting in an effective session length of 0. I've expanded my demo to include such a scenario, as well as multiple overlapping sessions:
WITH sessions AS
(SELECT uid,
GREATEST(time_start, COALESCE(MAX(time_end) OVER (PARTITION BY DATE(time_start) ORDER BY time_start ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING), '2000-01-01')) AS start,
MAX(time_end) OVER (PARTITION BY DATE(time_start) ORDER BY time_start ROWS UNBOUNDED PRECEDING) AS end
FROM sessions)
SELECT uid, DATE(start) AS `date`, SEC_TO_TIME(SUM(TO_SECONDS(end) - TO_SECONDS(start))) AS totaltime
FROM sessions
GROUP BY uid, `date`
输出:
uid date totaltime
1 2018-01-02 00:55:00
1 2018-01-03 01:00:00
1 2018-01-04 01:15:00
dbfiddle 演示
这篇关于不计算重叠的时间范围相加两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:不计算重叠的时间范围相加两次


- 导入具有可变标题的 Excel 文件 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- SQL 临时表问题 2022-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 更改自动增量起始编号? 2021-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01