MySQL:总结日期时间而不计算两次重叠时间

时间:2013-04-03 07:40:22

标签: mysql

我需要根据以下表格计算花费的总时间:

id | start_time | end_time |

期间可以重叠。我只需要计算一次ovelpapping周期。

E.g。如果我有这样的时期:

*----A----*              *------C-----* *----------D----------*
                  *-----B-----*              *---E---*

总和将是:(A.end-A.start) + (C.end - B.start) + (D.end - D.start)

我对编写此查询时应该使用的方法感到困惑,并且非常感谢您的帮助。

3 个答案:

答案 0 :(得分:2)

好的,我认真地坚持要你在生产中使用它之前以各种方式测试它。 特别是测试如果在1个时间跨度内存在MULTIPLE重叠会发生什么。

此查询的作用是计算每个时间跨度的持续时间,以及与具有更高ID的其他时间跨度存在多少重叠。

select
    t1.id,
    t1.start_time,
    t1.end_time,
    t1.end_time - t1.start_time as duration,
    sum(
          if(t2.start_time <  t1.start_time and t2.end_time >  t1.end_time  , t1.end_time - t1.start_time, 0) -- t2 completely around t1
        + if(t2.start_time >= t1.start_time and t2.end_time <= t1.end_time  , t2.end_time - t2.start_time, 0) -- t2 completely within t1
        + if(t2.start_time <  t1.start_time and t2.end_time >  t1.start_time and t2.end_time   < t1.end_time  , t2.end_time - t1.start_time, 0) -- t2 starts before t1 starts and overlaps partially
        + if(t2.start_time <  t1.end_time   and t2.end_time >  t1.end_time   and t2.start_time > t1.start_time, t1.end_time - t2.start_time, 0) -- t2 starts before t1 ends and overlaps partially
    ) as overlap
from
    times t1
    left join times t2 on
        t2.id > t1.id --  t2.id is greater than t1.id
        and (
               (t2.start_time <  t1.start_time and t2.end_time >  t1.end_time  ) -- t2 completely around t1
            or (t2.start_time >= t1.start_time and t2.end_time <= t1.end_time  ) -- t2 completely within t1
            or (t2.start_time <  t1.start_time and t2.end_time >  t1.start_time) -- t2 starts before t1 starts and overlaps
            or (t2.start_time <  t1.end_time   and t2.end_time >  t1.end_time  ) -- t2 starts before t1 ends and overlaps
        )
group by
    t1.id

所以你最终想要的是:

select
    sum(t.duration) - sum(t.overlap) as filtered_duration
from
    (
        OTHER QUERY HERE
    ) as t

所以最后你有这个问题:

select
    sum(t.duration) - sum(t.overlap) as filtered_duration
from
    (
        select
            t1.id,
            t1.start_time,
            t1.end_time,
            t1.end_time - t1.start_time as duration,
            sum(
                  if(t2.start_time <  t1.start_time and t2.end_time >  t1.end_time  , t1.end_time - t1.start_time, 0) -- t2 completely around t1
                + if(t2.start_time >= t1.start_time and t2.end_time <= t1.end_time  , t2.end_time - t2.start_time, 0) -- t2 completely within t1
                + if(t2.start_time <  t1.start_time and t2.end_time >  t1.start_time and t2.end_time   < t1.end_time  , t2.end_time - t1.start_time, 0) -- t2 starts before t1 starts and overlaps partially
                + if(t2.start_time <  t1.end_time   and t2.end_time >  t1.end_time   and t2.start_time > t1.start_time, t1.end_time - t2.start_time, 0) -- t2 starts before t1 ends and overlaps partially
            ) as overlap
        from
            times t1
            left join times t2 on
                t2.id > t1.id --  t2.id is greater than t1.id
                and (
                       (t2.start_time <  t1.start_time and t2.end_time >  t1.end_time  ) -- t2 completely around t1
                    or (t2.start_time >= t1.start_time and t2.end_time <= t1.end_time  ) -- t2 completely within t1
                    or (t2.start_time <  t1.start_time and t2.end_time >  t1.start_time) -- t2 starts before t1 starts and overlaps
                    or (t2.start_time <  t1.end_time   and t2.end_time >  t1.end_time  ) -- t2 starts before t1 ends and overlaps
                )
        group by
            t1.id
    ) as t

答案 1 :(得分:2)

我想建议另一种方法来获得时间,同时确保结果是正确的。但我不知道,如何完成MySQL的完成。

我将在以下时间重复使用上述示例 - 甚至可能存在第3级条目“F”:

1         3              7           12 13    (15 16)        20
|----A----|              |------C-----| |----------D----------|
                  |-----B-----|              |---E---|
                  5           9              14     17
                                                |F|
  1. 查询按时间排序的所有时间戳的组合列表,并添加每个“操作”的类型

    SELECT 1 as onoff, start_time as time FROM table
    UNION
    SELECT -1 as onoff, end_time as time FROM table
    ORDER BY time
    
  2. 使用临时计数器循环处理列表(?),在启动/登录时递增1,在结束/注销时递减1

    计数器应该使脚本在tmp.start=<time>的临时表中添加新行,如果它从0更改为1 并更新temp中上一行的tmp.end=<time>。表,如果它从1变为0.

    脚本会为上面的示例执行此操作,如下所示:

    QUERY                       TMP TABLE
    onoff | time  | ctr         ID | start | end
    1     | 01:00 | 1           1  | 01:00 |            (record 1 added,   ctr 0->1)
    -1    | 03:00 | 0           1  | 01:00 | 03:00      (record 1 updated, ctr 1->0)
    1     | 05:00 | 1           2  | 05:00 |            (record 2 added,   ctr 0->1)
    1     | 07:00 | 2                                   (nothing to do)
    -1    | 09:00 | 1                                   (nothing to do)
    -1    | 12:00 | 0           2  | 05:00 | 12:00      (record 2 updated, ctr 1->0)
    1     | 13:00 | 1           3  | 13:00 |            (record 3 added,   ctr 0->1)
    1     | 14:00 | 2                                   (nothing to do)
    1     | 15:00 | 3                                   (nothing to do)
    -1    | 16:00 | 2                                   (nothing to do)
    -1    | 17:00 | 1                                   (nothing to do)
    -1    | 20:00 | 0           3  | 13:00 | 20:00      (record 3 updated, ctr 1->0)
    
  3. 最后一步非常简单:从timestampdiff()start获取单位中的end,您需要/喜欢它并进行任何进一步过滤或分组

    例如:在其他地方使用数据

    SELECT ID, start, end, timestampdiff(MINUTE, start, end) FROM tmp
    

    或者例如:总结每个用户登录的工作时间/时间

    SELECT user_id, SUM(timestampdiff(MINUTE, start, end)) FROM tmp GROUP BY user_id
    
  4. 我敢肯定,这将为任何级别的嵌套提供正确的持续时间,但是有人知道如何在MySQL中完成此操作吗?我也想用它。

    祝你好运

    PS:如果脚本以&gt;结束,脚本也可能“关闭”上一个会话或抛出错误。如果计数器变为&lt; 1并抛出错误任何时候都是0

答案 2 :(得分:1)

我为another question写了一个类似的查询,所以我想我会根据这个问题调整它,任何人都有兴趣。

SELECT SUM(a.end_time - a.start_time) total_duration
  FROM (
    SELECT MIN(g.start_time) start_time, MAX(g.end_time) end_time 
      FROM (
        SELECT @group_id := @group_id + (@end_time IS NULL OR o.start_time > @end_time) group_id,
               start_time,
               @end_time := CAST(CASE 
                 WHEN (@end_time IS NULL OR o.start_time > @end_time) THEN o.end_time
                 ELSE GREATEST(o.end_time, @end_time)
               END AS DATETIME) end_time  
          FROM times o
          JOIN (SELECT @group_id := 0, @end_time := NULL) init
      ORDER BY o.start_time ASC  
            ) g
  GROUP BY  g.group_id  
        ) a

最内层查询将您的时间组合在重叠组中,在适当的时候延伸end_time。 end_time用于处理前一个完全包含的时间。

下一个换行查询从每个组中提取整个时间范围。

外部查询总结了每个组的差异。