2017-02-07 47 views
0

我有一個名爲時間(示例1)的mysql表,每天包含多個時間戳,它們表示登錄和註銷時間。開始停止差異按季度分組

第二表被稱爲time_intervals留下我與實施例2

我需要弄清楚該用戶是在通過15分鐘切片分組記錄的持續時間的輸出。

示例3顯示了我想要的結果。

實施例1(時間)

start_time  | end_time   | duration_s | user | 
---------------------------------------------------------------- 
2017-02-07 06:01:13 | 2017-02-07 07:03:19 | 3726  | x | 
2017-02-07 07:31:29 | 2017-02-07 08:00:00 | 3726  | x | 
2017............... ................... ....   . 

實施例2(time_intervals)

interval_start | interval_end  | 
-------------------------------------------- 
2017-02-07 06:00:00 | 2017-02-07 06:14:59 | 
2017-02-07 06:15:00 | 2017-02-07 06:29:59 | 
2017-02-07 06:30:00 | 2017-02-07 06:44:59 | 
2017-02-07 06:45:00 | 2017-02-07 06:59:59 | 
2017-02-07 07:00:00 | 2017-02-07 07:14:59 | 
2017............... | 2017............... 

實施例3(所希望的輸出)

interval_start  | interval_start  | duration_s | user | 
---------------------------------------------------------------- 
2017-02-07 06:00:00 | 2017-02-07 06:14:59 | 826 | x | 
2017-02-07 06:15:00 | 2017-02-07 06:29:59 | 899 | x | 
2017-02-07 06:30:00 | 2017-02-07 06:44:59 | 899 | x | 
2017-02-07 06:45:00 | 2017-02-07 06:59:59 | 899 | x | 
2017-02-07 07:00:00 | 2017-02-07 07:14:59 | 199 | x | 
2017-02-07 07:15:00 | 2017-02-07 07:30:59 | 0  | x | 
2017-02-07 07:30:00 | 2017-02-07 07:44:59 | 810 | x | 

任何形式的幫助,將不勝感激。應努力提供

回答

0

像下面這time_intervals包含所有15個分鐘的間隔來顯示:

SELECT ti.interval_start, 
     ti.interval_end,  
     TIMESTAMPDIFF(SECOND, GREATEST(t.start_time, ti.interval_start), 
          LEAST(t.end_time, ti.interval_end)) AS duration_s 
FROM time_intervals AS ti 
LEFT JOIN time AS t 
    ON t.start_time <= ti.interval_end AND t.end_time >= ti.interval_start 
+0

「規定,time_intervals包含所有15個分鐘的間隔」哼。 – Strawberry