Sql 以错误的输出打开

Sql 以错误的输出打开,sql,database,postgresql,Sql,Database,Postgresql,输出是ID2,应该是ID3 我做错了什么?我只对这种类型的查询感兴趣,它的在上不同,请尝试以下操作: select DISTINCT ON (DATE(dt)) id, dt from test ORDER BY DATE(dt) DESC 您必须按dt添加订单才能获得一致的结果: select DISTINCT ON (DATE(dt)) id, dt from test ORDER BY DATE(dt)

输出是ID2,应该是ID3

我做错了什么?我只对这种类型的查询感兴趣,它的

上不同,请尝试以下操作:

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

您必须按
dt
添加订单才能获得一致的结果:

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

select DISTINCT ON (DATE(dt))
       id, dt
       from test
       ORDER BY DATE(dt) DESC, dt DESC
select distinct on (date(dt))
    id, dt
from test
order by date(dt) desc, dt desc