2016-04-13 125 views
0

我試圖從日期時間列中使用10分鐘的時間間隔獲取平均值。所以,胖我得到這個SQL與間隔MySQL以10分鐘的時間間隔按日期時間分組得到平均值

     select 1 AS `location_id`, 
     `raw_datas`.`parsing_at` AS `date`, 
count(`raw_datas`.`station_mac`) AS `quantity` 
           FORM `raw_datas` 
          WHERE (timediff(`raw_datas`.`parsing_at`,`raw_datas`.`last_time_seen`) <= 20) 
          GROUP BY UNIX_TIMESTAMP(`raw_datas`.`parsing_at`) DIV 600 
          order by `raw_datas`.`parsing_at`; 

但結果得到的數據顯示添加

+-------------+---------------------+----------+ 
| location_id | date    | quantity | 
+-------------+---------------------+----------+ 
|   1 | 2016-04-11 05:10:22 |  137 | 
|   1 | 2016-04-11 05:20:02 |  121 | 
|   1 | 2016-04-11 05:30:02 |  24 | 
|   1 | 2016-04-11 06:06:11 |  20 | 
|   1 | 2016-04-11 06:10:02 |  36 | 
|   1 | 2016-04-11 06:20:02 |  37 | 
|   1 | 2016-04-11 06:30:02 |  29 | 
|   1 | 2016-04-11 08:46:10 |  7 | 
|   1 | 2016-04-11 08:50:02 |  21 | 
|   1 | 2016-04-11 09:00:22 |  16 | 
|   1 | 2016-04-11 09:10:22 |  16 | 
|   1 | 2016-04-11 09:20:02 |  15 | 
|   1 | 2016-04-11 09:30:02 |  8 | 
|   1 | 2016-04-11 09:41:01 |  1 | 
|   1 | 2016-04-11 15:01:02 |  69 | 
|   1 | 2016-04-11 15:10:02 |  106 | 
|   1 | 2016-04-11 15:20:02 |  69 | 
|   1 | 2016-04-11 23:18:02 |  20 | 
|   1 | 2016-04-11 23:20:02 |  124 | 
|   1 | 2016-04-11 23:30:02 |  105 | 
|   1 | 2016-04-11 23:40:02 |  80 | 
|   1 | 2016-04-12 22:00:35 |  224 | 
|   1 | 2016-04-12 22:10:02 |  191 | 
|   1 | 2016-04-12 22:20:02 |  193 | 
|   1 | 2016-04-12 22:30:02 |  66 | 
+-------------+---------------------+----------+ 

我怎樣才能讓數量列上顯示的平均值的時間間隔日期欄?

回答

2

您可以使用AVG()函數代替count()來獲得平均值