Question

I have a table that looks like this

+--------------------+-------+
| Date/Time          | Value |
+--------------------+-------+
| 2014-01-01 8:30:10 |  10   |
| 2014-01-01 8:30:05 |  11   |
| 2014-01-01 7:15:04 |  24   | 
| 2014-01-01 7:00:01 |  15   |
| 2014-01-01 6:55:20 |  06   | 
| 2014-01-01 6:30:10 |  38   |
+--------------------+-------+

And I'd want to get the values of that day but in a hour interval, how should I do it? I'd want the result to be something like this:

+--------------------+-------+
| Date/Time          | Value |
+--------------------+-------+
| 2014-01-01 8:30:10 |  10   |
| 2014-01-01 7:15:04 |  24   | 
| 2014-01-01 6:55:20 |  06   | 
+--------------------+-------+
Was it helpful?

Solution

SELECT x.* 
 FROM my_table x 
 JOIN 
    ( SELECT MAX(dt) max_dt 
        FROM my_table 
       GROUP BY DATE(dt),HOUR(dt)
    ) y
   ON y.max_dt = x.dt;

Note that this assumes that dt is PRIMARY (or at the very least UNIQUE)

DEMO

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top