开发者

SQL query to get single row value from an aggregate

I have an Oracle table with two columns ID and START_DATE, I want to run a query to get the ID of the record with the most recent date, initially i wrote this:

 select id from (select * from mytable order by start_date desc) where rownum = 1
开发者_开发知识库

Is there a more cleaner and efficient way of doing this? I often run into this pattern in SQL and end up creating a nested query.


SELECT id FROM mytable WHERE start_date = (SELECT MAX(start_date) FROM mytable)

Still a nested query, but more straightforward and also, in my experience, more standard.


This looks to be a pretty clean and efficient solution to me - I don't think you can get any better than that, of course assuming that you've an index on start_date. If you want all ids for the latest start date then froadie's solution is better.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜