2014-09-30 46 views
1
3 2014-09-30 16:07:18.690498 
2 2014-09-30 16:06:30.224745 
1 2014-09-30 16:04:45.844215 

select DISTINCT ON (DATE(dt)) 
     id, dt 
     from test 
     ORDER BY DATE(dt) DESC 

輸出是id 2,應該是ID爲3DISTINCT與錯誤的輸出

我做錯了嗎?我只對DISTINCT ON這種查詢感興趣。

回答

1

嘗試類似:

select DISTINCT ON (DATE(dt)) 
     id, dt 
     from test 
     ORDER BY DATE(dt) DESC, dt DESC 
1

你必須dt添加爲了得到一致的結果:

select distinct on (date(dt)) 
    id, dt 
from test 
order by date(dt) desc, dt desc 

sql fiddle demo