2017-03-27 63 views
0

我試圖按7天的時間間隔對數據進行分組。例如Mysql按7天間隔選擇查詢組

我有一個數據,你可以在下面找到它。

count startDate   finish_date   
1247 2017-03-09 08:43:18 2017-03-09 16:05:34 
1681 2017-03-10 08:30:13 2017-03-10 16:31:55 
1464 2017-03-11 08:36:50 2017-03-11 16:42:03 
1343 2017-03-12 08:26:57 2017-03-12 16:39:58 
1333 2017-03-13 08:35:34 2017-03-13 16:26:18 
1215 2017-03-14 08:36:58 2017-03-14 16:13:20 
1817 2017-03-16 08:24:49 2017-03-16 17:18:19 
1675 2017-03-17 08:22:30 2017-03-17 16:36:58 
1546 2017-03-18 08:33:52 2017-03-18 16:51:52 
1443 2017-03-20 08:11:00 2017-03-20 16:26:38 
1481 2017-03-21 08:26:04 2017-03-21 16:57:30 
1574 2017-03-23 08:19:07 2017-03-23 16:12:46 
1270 2017-03-24 08:25:25 2017-03-24 16:37:59 
1765 2017-03-25 08:22:58 2017-03-25 16:44:24 
1200 2017-03-26 08:37:47 2017-03-26 14:59:51 
1479 2017-03-27 08:17:50 2017-03-27 15:18:32 

而且我想按7天的間隔對它們進行分組。 我試過了。爲了它。

select count(*), min(locationDate) as startDate, max(locationDate) as finish_date from location where tagCode = 24901 and xLocation >= 278 and xLocation <= 354 and yLocation >= 239 and yLocation <= 426 and locationDate 
>= DATE_SUB('2017-03-01 00:00:01',INTERVAL 7 day) and locationDate <= '2017-03-27 23:59:59' group by DATEDIFF(locationDate, '2017-03-01 00:00:01') div 7 

而數據就像。

count startDate   finish_date   
8283 2017-03-09 08:43:18 2017-03-14 16:13:20 
7962 2017-03-16 08:24:49 2017-03-21 16:57:30 
7291 2017-03-23 08:19:07 2017-03-27 15:22:05 

問題是第二週,必須從2017年3月15日開始,第三個星期需要啓動2017年3月22日卻因爲沒有在天無數據的不啓動怎麼解決呢?

+0

如果您查詢的是列,那麼表中的數據中沒有第15個和第22個日期。 –

+0

7天的時間間隔是否讓您以9,16和23開頭的小組不正確? –

回答

1

正如我在我的評論中問你的,我認爲你寫的結果對你提供的輸入會很好,但不會有不同的輸入(如有2017-03-15而不是2017-03-16)。

一個解決辦法是寫一種像這樣

select sum(count) as count, min(location_date), max(location_date) 
from (
      select t1.location_date, 
        t1.count, 
        date_sub(location_date, interval (datediff(t1.location_date, t2.min_date) % 7) day) week_start 
      from location t1 
      cross join 
        (select min(location_date) as min_date from location) t2 
      where t1.tagCode = 24901 and 
        t1.xLocation between 278 and 354 and 
        t1.yLocation between 239 and 426 and 
        t1.locationDate >= DATE_SUB('2017-03-01 00:00:01',INTERVAL 7 day) and 
        t1.locationDate <= '2017-03-27 23:59:59' 
        ) t3 
group by week_start 

我測試的這一個簡化版本對您輸入的簡化版本查詢,可能會有錯別字......

編輯

同時顯示間隔開始日期和結束日期,嘗試用這種

select sum(count) as count, week_start, week_end 
from (
      select t1.count, 
        date_sub(location_date, interval (datediff(t1.location_date, t2.min_date) % 7) day) week_start, 
        date_sub(location_date, interval (datediff(t1.location_date, t2.min_date) % 7) - 6 day) week_end 
      from location t1 
      cross join 
        (select min(location_date) as min_date from location) t2 
      where t1.tagCode = 24901 and 
        t1.xLocation between 278 and 354 and 
        t1.yLocation between 239 and 426 and 
        t1.locationDate >= DATE_SUB('2017-03-01 00:00:01',INTERVAL 7 day) and 
        t1.locationDate <= '2017-03-27 23:59:59' 
        ) t3 
group by week_start, week_end 
+0

我的評論有點長,所以我把它作爲一個帖子。 –

+0

我創建了一個工作rextester [這裏](http://rextester.com/UATYV54399),看看它 –

+0

謝謝它適用於開始日期,但我需要使完成時間相同的東西。你會建議什麼? @stefanozanini –

0

我認爲你可以這樣做: 你需要從這個改變您的查詢的結果:使用計算最大日期和最小日期之間的天數的邏輯

1 1247 2017-03-09 08:43:18 2017-03-09 16:05:34 
2 1681 2017-03-10 08:30:13 2017-03-10 16:31:55 
3 1464 2017-03-11 08:36:50 2017-03-11 16:42:03 
4 1343 2017-03-12 08:26:57 2017-03-12 16:39:58 
5 1333 2017-03-13 08:35:34 2017-03-13 16:26:18 
6 1215 2017-03-14 08:36:58 2017-03-14 16:13:20 
7 1817 2017-03-16 08:24:49 2017-03-16 17:18:19 
8 1675 2017-03-17 08:22:30 2017-03-17 16:36:58 
9 1546 2017-03-18 08:33:52 2017-03-18 16:51:52 
10 1443 2017-03-20 08:11:00 2017-03-20 16:26:38 
11 1481 2017-03-21 08:26:04 2017-03-21 16:57:30 
12 1574 2017-03-23 08:19:07 2017-03-23 16:12:46 
13 1270 2017-03-24 08:25:25 2017-03-24 16:37:59 
14 1765 2017-03-25 08:22:58 2017-03-25 16:44:24 
15 1200 2017-03-26 08:37:47 2017-03-26 14:59:51 
16 1479 2017-03-27 08:17:50 2017-03-27 15:18:32 

到這第一行:

  -- max date of the row   min date of the first row 
select FLOOR(datediff('2017-03-12 16:05:34', '2017-03-09 08:43:18')/7); 

select FLOOR(datediff('2017-03-16 17:18:19', '2017-03-09 08:43:18')/7); 

-- what is important that you always compute the max date - the min date of the first row the same row like in your example is : '2017-03-09 08:43:18' 
select FLOOR(datediff(max_date, '2017-03-09 08:43:18')/7); 

    rec_sum  min_date   max_date   day_diff 
1 1247 2017-03-09 08:43:18 2017-03-09 16:05:34 0 
2 1681 2017-03-10 08:30:13 2017-03-10 16:31:55 0 
3 1464 2017-03-11 08:36:50 2017-03-11 16:42:03 0 
4 1343 2017-03-12 08:26:57 2017-03-12 16:39:58 0 
5 1333 2017-03-13 08:35:34 2017-03-13 16:26:18 0 
6 1215 2017-03-14 08:36:58 2017-03-14 16:13:20 0 
7 1817 2017-03-16 08:24:49 2017-03-16 17:18:19 1 
8 1675 2017-03-17 08:22:30 2017-03-17 16:36:58 1 
9 1546 2017-03-18 08:33:52 2017-03-18 16:51:52 1 
10 1443 2017-03-20 08:11:00 2017-03-20 16:26:38 1 
11 1481 2017-03-21 08:26:04 2017-03-21 16:57:30 1 
12 1574 2017-03-23 08:19:07 2017-03-23 16:12:46 2 
13 1270 2017-03-24 08:25:25 2017-03-24 16:37:59 2 
14 1765 2017-03-25 08:22:58 2017-03-25 16:44:24 2 
15 1200 2017-03-26 08:37:47 2017-03-26 14:59:51 2 
16 1479 2017-03-27 08:17:50 2017-03-27 15:18:32 2 

-- now you can group the new result by the new field the result of division. 

select 
     sum(rec_sum) , 
     min(min_date), 
     max(max_date) 
from (query result in the previous list) 
group by day_diff 

i know it's a little bit hard but i think you can do it the hard way is the day_diff computing . 
+0

如果你不明白你可以把你的表結構和你第一次查詢 – Cherif