SQL - How to select a row having a column with max value

前端 未结 9 781
悲&欢浪女
悲&欢浪女 2020-11-29 02:38
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/20         


        
相关标签:
9条回答
  • 2020-11-29 02:44

    Answer is to add a having clause:

    SELECT [columns]
    FROM table t1
    WHERE value= (select max(value) from table)
    AND date = (select MIN(date) from table t2 where t1.value = t2.value)
    

    this should work and gets rid of the neccesity of having an extra sub select in the date clause.

    0 讨论(0)
  • 2020-11-29 02:47

    In Oracle:

    This gets the key of the max(high_val) in the table according to the range.

    select high_val, my_key
    from (select high_val, my_key
          from mytable
          where something = 'avalue'
          order by high_val desc)
    where rownum <= 1
    
    0 讨论(0)
  • 2020-11-29 02:49

    Technically, this is the same answer as @Sujee. It also depends on your version of Oracle as to whether it works. (I think this syntax was introduced in Oracle 12??)

    SELECT *
    FROM   table
    ORDER BY value DESC, date_column ASC
    FETCH  first 1 rows only;
    

    As I say, if you look under the bonnet, I think this code is unpacked internally by the Oracle Optimizer to read like @Sujee's. However, I'm a sucker for pretty coding, and nesting select statements without a good reason does not qualify as beautiful!! :-P

    0 讨论(0)
  • 2020-11-29 02:50

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

    http://en.wikipedia.org/wiki/Select_(SQL)#Result_limits

    Oracle: ROWNUM could be used.

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

    Answering the question more specifically:

    select high_val, my_key
    from (select high_val, my_key
          from mytable
          where something = 'avalue'
          order by high_val desc)
    where rownum <= 1
    
    0 讨论(0)
  • 2020-11-29 02:55

    Analytics! This avoids having to access the table twice:

    SELECT DISTINCT
           FIRST_VALUE(date_col)  OVER (ORDER BY value_col DESC, date_col ASC),
           FIRST_VALUE(value_col) OVER (ORDER BY value_col DESC, date_col ASC)
    FROM   mytable;
    
    0 讨论(0)
  • 2020-11-29 02:57
    SQL> create table t (mydate,value)
      2  as
      3  select to_date('18/5/2010, 1 pm','dd/mm/yyyy, hh am'), 40 from dual union all
      4  select to_date('18/5/2010, 2 pm','dd/mm/yyyy, hh am'), 20 from dual union all
      5  select to_date('18/5/2010, 3 pm','dd/mm/yyyy, hh am'), 60 from dual union all
      6  select to_date('18/5/2010, 4 pm','dd/mm/yyyy, hh am'), 30 from dual union all
      7  select to_date('18/5/2010, 5 pm','dd/mm/yyyy, hh am'), 60 from dual union all
      8  select to_date('18/5/2010, 6 pm','dd/mm/yyyy, hh am'), 25 from dual
      9  /
    
    Table created.
    
    SQL> select min(mydate) keep (dense_rank last order by value) mydate
      2       , max(value) value
      3    from t
      4  /
    
    MYDATE                   VALUE
    ------------------- ----------
    18-05-2010 15:00:00         60
    
    1 row selected.
    

    Regards, Rob.

    0 讨论(0)
提交回复
热议问题