Salustiano Muniz
Salustiano Muniz

Reputation: 322

MySQL get average value grouping by datetime in 10 minute interval

I'm trying to get a the average value using a 10 minute interval from a datetime column. So fat I got this SQL to get the data with the interval

                        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`;

But the results are shown added

+-------------+---------------------+----------+
| 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 |
+-------------+---------------------+----------+

How can I make the quantity column to show the average values for the time intervals on date column?

Upvotes: 0

Views: 415

Answers (1)

Arjun J Gowda
Arjun J Gowda

Reputation: 730

You can use AVG() function instead of count() to get average

Upvotes: 2

Related Questions