arkhon
arkhon

Reputation: 765

Count data for 15 minute intervals that are grouped by date

I have the following MySQL table:

+---------------------+-----------+
|      timestamp      | sensor_id |
+---------------------+-----------+
| 2010-04-09 01:42:31 |    M049   |
| 2010-04-09 01:43:31 |    M049   |
| 2010-04-09 01:44:31 |    M049   |
| 2010-04-09 01:59:31 |    M049   |
| 2010-04-10 01:10:31 |    M049   |
| 2010-04-10 01:40:31 |    M049   |
| 2010-04-10 01:42:31 |    M049   |
| 2010-04-11 16:43:31 |    M049   |
+---------------------+-----------+

I know how to query the db to get a count of the entries for a specific daytime intervall and group the result by date.

An example to query the event count between 1 am and 2 pm would look like this:

SELECT
    date(timestamp) as date,
    count(timestamp) as count
FROM 
    event_data
WHERE 
    EXTRACT(HOUR FROM TIME(timestamp)) BETWEEN 1 AND 14
GROUP BY 
    date

The query returns the following table:

+------------+-------+
|    date    | count |
+------------+-------+
| 2010-04-09 |   4   |
| 2010-04-10 |   3   |
+------------+-------+

Now I only want to count an event every 15 minutes. The desired result would be:

+------------+-------+
|    date    | count |
+------------+-------+
| 2010-04-09 |   2   |
| 2010-04-10 |   2   |
+------------+-------+

How do I alter my query to get these results?

Upvotes: 2

Views: 1915

Answers (1)

AdamMc331
AdamMc331

Reputation: 16690

You have a good start to group by date and query for the hours you want. Similarly, you can write a query that gets the intervals. I would start by writing a case statement that reads each row you want, and adds a column specifying which interval of the hour it is. (0:14 - 1, 15:29 - 2...) like this:

SELECT timeCol, 
  HOUR(timeCol) AS hour,
  CASE WHEN MINUTE(timeCol) BETWEEN 0 AND 14 THEN 1
  WHEN MINUTE(timeCol) BETWEEN 15 AND 29 THEN 2
  WHEN MINUTE(timeCol) BETWEEN 30 AND 44 THEN 3
  ELSE 4 END AS minute
FROM myTable;

This gives you something like this:

|      timeCol        | hour | minute |
+---------------------+------+--------+
| 2010-04-09 01:42:31 |  1   |   3    |
| 2010-04-09 01:43:31 |  1   |   3    |
| 2010-04-09 01:44:31 |  1   |   3    |

Once you have that, you can select the distinct hour/minute pairs in each day, and that will give you what you want, as long as you use your WHERE clause accordingly:

SELECT DATE(timeCol) AS dateCol, COUNT(DISTINCT hour, minute) AS numEvents
FROM(
  SELECT timeCol, 
    HOUR(timeCol) AS hour,
    CASE WHEN MINUTE(timeCol) BETWEEN 0 AND 14 THEN 1
    WHEN MINUTE(timeCol) BETWEEN 15 AND 29 THEN 2
    WHEN MINUTE(timeCol) BETWEEN 30 AND 44 THEN 3
    ELSE 4 END AS minute
  FROM myTable) tmp
WHERE HOUR(timecol) BETWEEN 1 AND 14
GROUP BY dateCol;

Here is an SQL Fiddle example.

I would just like to add that you don't have to record the intervals as 1, 2, 3, 4. Make sure you use something readable, that will make sense to you again in the future. For example, maybe something like this would be better:

WHEN MINUTE(timeCol) BETWEEN 0 and 14 THEN 'firstInterval'...

Upvotes: 2

Related Questions