SQL - 如何选择具有最大值的列的行

2021-12-06 00:00:00 sql oracle
date                 value

18/5/2010, 1 pm        40
18/5/2010, 2 pm        20
18/5/2010, 3 pm        60
18/5/2010, 4 pm        30
18/5/2010, 5 pm        60
18/5/2010, 6 pm        25 

我需要查询具有 max(value)(即 60)的行.所以,这里我们得到两行.从那以后,我需要当天具有最低时间戳的行(即 18/5/2010,下午 3 点 -> 60)

i need to query for the row having max(value)(i.e. 60). So, here we get two rows. From that, I need the row with the lowest time stamp for that day(i.e 18/5/2010, 3 pm -> 60)

推荐答案

TOP、LIMIT、ROWNUM 等关键字依赖于数据库.请阅读这篇文章了解更多信息.

Keywords like TOP, LIMIT, ROWNUM, ...etc are database dependent. Please read this article for more information.

http://en.wikipedia.org/wiki/Select_(SQL)#结果限制

Oracle:可以使用 ROWNUM.

Oracle: ROWNUM could be used.

select * from (select * from table 
order by value desc, date_column) 
where rownum = 1;

更具体地回答问题:

select high_val, my_key
from (select high_val, my_key
      from mytable
      where something = 'avalue'
      order by high_val desc)
where rownum <= 1

相关文章