How to select records with maximum values in two columns?

后端 未结 2 831
生来不讨喜
生来不讨喜 2020-11-30 13:41

It was hard to come up with an understandable title for this question. I\'ll try to explain with an example.

First I had a simple table INFO in Oracle

2条回答
  •  我在风中等你
    2020-11-30 14:00

    Analytic functions are your friend:

    SELECT   MAX( year    ) KEEP ( DENSE_RANK LAST ORDER BY year ASC, quarter ASC, message ASC ) AS year,
             MAX( quarter ) KEEP ( DENSE_RANK LAST ORDER BY year ASC, quarter ASC, message ASC ) AS quarter,
             MAX( message ) KEEP ( DENSE_RANK LAST ORDER BY year ASC, quarter ASC, message ASC ) AS message,
             type
    FROM     info
    GROUP BY type;
    

    SQLFIDDLE

提交回复
热议问题