我想获取max last_updated_date的id /行,但查询返回相同的行数
id | job_number | last_udated_date
1407;"88212";"2015-12-16 00:00:00+05:30"
77;"48439";"2015-12-16 00:00:00+05:30"
1526;"1484924208";"2017-01-21 00:00:00+05:30"
1527;"1484924208";"2017-01-21 00:00:00+05:30"
1528;"1484924208";"2017-01-21 00:00:00+05:30"
1529;"1484924208";"2017-01-22 07:20:42.586+05:30"
查询应仅返回1407,77,1529,但现在返回所有值
查询:
select id, last_updated_date
from brs.events
where last_updated_date in (select max(last_updated_date)
from brs.events
group by last_updated_date )
答案 0 :(得分:1)
你想这样做:
SELECT id,
last_updated_date
FROM brs.events
WHERE last_updated_date =
(SELECT MAX(last_updated_date) FROM brs.events
)
如果你想根据最新的last_update_date获得一行,你可以使用LIMIT:
select id
from brs.events
order by last_update_date desc
limit 1;
更新,了解问题的最新变化:
select
id,
last_updated_date
from (
select
t.*,
row_number() over (partition by job_number order by last_updated_date desc) rn
from your_table t
) t where rn = 1;
答案 1 :(得分:1)
如果我理解正确,理想的选项是DISTINCT ON()
,因为它使查询变得非常简单。
SELECT DISTINCT ON(表达式[,...])仅保留第一行 给定表达式求值等于
的每组行
select distinct on (job_no) id
,job_no
,max(lpd)
from evnts
group by job_no,id
此处,Distinct on
将评估每个组中的行数(job_number
)