2014-03-06 65 views
0

我有一個表,看起來像這樣獲取導致間隔一小時

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

所以你要每小時最新一排? – Strawberry

+0

@Strawberry是 – hearmeroar

+0

http://stackoverflow.com/questions/6137024/mysql-how-to-group-data-per-hour-and-get-the-latest-hour – user1844933

回答

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(或者至少是唯一的)

DEMO

+1

+1不錯:)。我添加了小提琴,如果你介意:) –

+0

不要介意! – Strawberry

+0

謝謝,工作! :) – hearmeroar