以一小时的间隔获得结果

时间:2014-03-06 19:02:03

标签: mysql intervals

我有一张看起来像这样的表

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

我想要获得当天的价值,但是在一小时的间隔内,我该怎么做? 我希望结果是这样的:

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

1 个答案:

答案 0 :(得分:3)

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;

请注意,这假设dt是PRIMARY(或至少是UNIQUE)

DEMO