开发者

How can I select the "maximum" row from a table?

开发者 https://www.devze.com 2023-01-12 23:36 出处:网络
How can I select the maximum row from a table?What does maximum mean -- well my table has two timestamp columns, TIME1 and TIME2.The maximum column is the o开发者_开发百科ne with the latest value for

How can I select the maximum row from a table? What does maximum mean -- well my table has two timestamp columns, TIME1 and TIME2. The maximum column is the o开发者_开发百科ne with the latest value for TIME1. If that is not a unique row, then the maximum is the one within those rows with the latest value for TIME2.

This is on Oracle if that matters.


What you need is a "Top-N" query:

select * from ( select * from table order by time1 desc, time2 desc ) where rownum < 2;

if you properly index on time1, time2 it will be very fast:

http://blog.fatalmind.com/2010/07/30/analytic-top-n-queries/

0

精彩评论

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