Multiple max values in a query

前端 未结 6 1904
余生分开走
余生分开走 2021-01-12 21:08

I know the title does not sound very descriptive, but it is the best I could think of:

I have this table

ID     BDATE      VALUE
28911  14/4/2009  44820
2         


        
6条回答
  •  再見小時候
    2021-01-12 21:27

    you can use the MAX...KEEP(DENSE_RANK FIRST...) construct:

    SQL> SELECT ID,
      2         MAX(bdate) bdate,
      3         MAX(VALUE) KEEP(DENSE_RANK FIRST ORDER BY bdate DESC) VALUE 
      4   FROM DATA
      5  GROUP BY ID;
    
            ID BDATE            VALUE
    ---------- ----------- ----------
         28911 24/04/2009  7749594,67
         38537 22/04/2009    81098692
         38605 23/04/2009     6936575
    

    This will be as efficient as the analytics method suggested by Majkel (no self-join, a single pass on the data)

提交回复
热议问题