时间重叠总和
Time Overlapping sum
我有一个 table 结构如下:
id_task INTEGER,
task_begin DATETIME,
task_end DATETIME
我有这样的行:
id_task task_begin task_end
1 2015-03-23 09:00:00 2015-03-23 09:14:00
2 2015-03-23 09:10:00 2015-03-23 09:16:00
3 2015-03-23 09:18:00 2015-03-23 09:24:00
4 2015-03-23 09:21:00 2015-03-23 09:23:00
5 2015-03-23 09:24:00 2015-03-23 09:26:00
6 2015-03-23 09:26:00 2015-03-23 09:45:00
7 2015-03-23 09:30:00 2015-03-23 09:36:00
8 2015-03-23 09:34:00 2015-03-23 09:39:00
9 2015-03-23 09:05:00 null
10 null null
11 2015-03-23 10:02:00 2015-03-23 10:14:00
12 2015-03-23 10:04:00 2015-03-23 10:10:00
13 2015-03-23 10:03:00 2015-03-23 10:13:00
14 2015-03-23 10:10:00 null
15 2015-03-23 10:20:00 2015-03-23 10:30:00
16 2015-03-23 10:40:00 2015-03-23 10:50:00
我想select实际工作时间不重叠。结果将是这样的:
task_begin task_end
2015-03-23 09:00:00 2015-03-23 09:16:00
2015-03-23 09:18:00 2015-03-23 09:45:00
2015-03-23 10:02:00 2015-03-23 10:14:00
2015-03-23 10:20:00 2015-03-23 10:30:00
2015-03-23 10:40:00 2015-03-23 10:50:00
如何使用 SELECT 或 MySQL 中的过程来完成?
SELECT MIN(task_begin) AS task_begin, MAX(task_end) AS task_end
FROM (
SELECT
t.*,
@rc := IF(@prev_task_end >= task_begin, @rc, @rc + 1) AS rc,
@prev_task_end := task_end
FROM
t,
(SELECT @rc := 0, @prev_task_end := null) var_init_subquery
ORDER BY id_task
) sq
GROUP BY rc
HAVING task_begin IS NOT NULL OR task_end IS NOT NULL
我有一个 table 结构如下:
id_task INTEGER,
task_begin DATETIME,
task_end DATETIME
我有这样的行:
id_task task_begin task_end
1 2015-03-23 09:00:00 2015-03-23 09:14:00
2 2015-03-23 09:10:00 2015-03-23 09:16:00
3 2015-03-23 09:18:00 2015-03-23 09:24:00
4 2015-03-23 09:21:00 2015-03-23 09:23:00
5 2015-03-23 09:24:00 2015-03-23 09:26:00
6 2015-03-23 09:26:00 2015-03-23 09:45:00
7 2015-03-23 09:30:00 2015-03-23 09:36:00
8 2015-03-23 09:34:00 2015-03-23 09:39:00
9 2015-03-23 09:05:00 null
10 null null
11 2015-03-23 10:02:00 2015-03-23 10:14:00
12 2015-03-23 10:04:00 2015-03-23 10:10:00
13 2015-03-23 10:03:00 2015-03-23 10:13:00
14 2015-03-23 10:10:00 null
15 2015-03-23 10:20:00 2015-03-23 10:30:00
16 2015-03-23 10:40:00 2015-03-23 10:50:00
我想select实际工作时间不重叠。结果将是这样的:
task_begin task_end
2015-03-23 09:00:00 2015-03-23 09:16:00
2015-03-23 09:18:00 2015-03-23 09:45:00
2015-03-23 10:02:00 2015-03-23 10:14:00
2015-03-23 10:20:00 2015-03-23 10:30:00
2015-03-23 10:40:00 2015-03-23 10:50:00
如何使用 SELECT 或 MySQL 中的过程来完成?
SELECT MIN(task_begin) AS task_begin, MAX(task_end) AS task_end
FROM (
SELECT
t.*,
@rc := IF(@prev_task_end >= task_begin, @rc, @rc + 1) AS rc,
@prev_task_end := task_end
FROM
t,
(SELECT @rc := 0, @prev_task_end := null) var_init_subquery
ORDER BY id_task
) sq
GROUP BY rc
HAVING task_begin IS NOT NULL OR task_end IS NOT NULL