How can I select the “maximum” row from a table?

亡梦爱人 提交于 2019-12-24 09:49:52

问题


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 one 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.


回答1:


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/



来源:https://stackoverflow.com/questions/3573996/how-can-i-select-the-maximum-row-from-a-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!