MySQL - 按15分钟间隔分组时间范围

时间:2018-05-17 20:55:49

标签: mysql sql time intervals

所以,我有一个计时表,跟踪我的员工花在登录不同州的时间。它看起来像这样:

Employee_ID     Queue             StartTime            End Time
   121509      'Break'      2018-05-17 13:31:54  2018-05-17 13:47:02
   121509      'Working'    2018-05-17 13:47:04  2018-05-17 15:05:45
   121509      'Unavail.'   2018-05-17 15:05:46  2018-05-17 15:32:01

等等。我的目标是聚合所有员工在每个队列状态中花费的时间(通过从EndTime中减去StartTime找到)并将该聚合分组十五分钟。我 做的是:

concat(
    time_format(convert_tz(StartTime,'UTC','America/Denver'),'%H'),
    ':',
    case
        when minute(convert_tz(StartTime,'UTC','America/Denver')) < 15
            then '00'
        when minute(convert_tz(StartTime,'UTC','America/Denver')) < 30
            then '15'
        when minute(convert_tz(StartTime,'UTC','America/Denver')) < 45
            then '30'
        else '45'
    end,
    ':00'
) as 'Interval',

但是,通过这种方式分组,我意识到如果我按这种方式分组,那么在队列中花费的任何时间都只会计入员工登录的第一个间隔,而不是分裂在StartTime和Endtime之间布置的所有时间间隔之间的时间。

所以,我的问题是:如何对数据进行分组,以便如果给定的时间范围超过它开始的十五分钟间隔,那么它会开始计算 next 十五分钟的间隔?

示例输出:

Employee_ID   Interval      Queue    QueueTime
   121509        13:30      'Break'   00:14:54
   121509        13:45      'Break'   00:02:02
   121509        13:45     'Working'  00:13:58
   121509        14:00     'Working'  00:15:00
   121509        14:15     'Working'  00:15:00
   121509        14:30     'Working'  00:02:58
   121509        14:30     'Unavail.' 00:08:13
   121509        14:30     'Break'    00:03:28

2 个答案:

答案 0 :(得分:0)

我猜你想要获得时间戳差异,然后按员工和队列类型分组。像这样的查询应该适合您:

SELECT 
    Employee_ID,
    Queue,
    SUM(TIMESTAMPDIFF(MINUTE,
        StartTime,
        EndTime)) AS Time
FROM
    EmployeeTable
GROUP BY Employee_ID , Queue;

答案 1 :(得分:0)

好的。这花了一些时间,但我想通过RToyo的帮助来解决这个问题:

首先:创建一个十五分钟间隔的列表:

filter {
       # pattern matching logback pattern
       grok {
              match => { "message" => "%{TIMESTAMP_ISO8601:timestamp}\s+%{LOGLEVEL:severity}\s+\[%{DATA:service},%{DATA:trace},%{DATA:span},%{DATA:exportable}\]\s+%{DATA:pid}\s+---\s+\[%{DATA:thread}\]\s+%{DATA:class}\s+:\s+%{GREEDYDATA:rest}" }
       }
       json{
              source => "message"
       }
}

下一步:通过测试StartTime和Endtime字段之间的重叠来加入Timekeeping表:

output {
    elasticsearch { 
         hosts => ["localhost:9200"] 
         index => "YOUR_INDEX_NAME-%{+YYYY.MM.dd}"
    }
} 

然后只需将StartTime和EndTime字段修改为以间隔为界限:

        select 
        addtime(time('00:00:00'),sec_to_time(900 * (a.a + b.a * 10))) as 'IntervalStart',
        addtime(time('00:14:59'),sec_to_time(900 * (a.a + b.a * 10))) as 'IntervalEnd'
    from (
        select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
        cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
    where
        addtime(time('00:00:00'),sec_to_time(900 * (a.a + b.a * 10))) between '06:00:00' and '18:45:00'