Skip to content
Advertisement

Max Value based on two columns

I have a table with multiple columns that contains both month and year. I’m trying to identify based on column1,column2 what is the greatest month and year.

I have a DDL for a test table as such.

This is the data in the example table.

I want to get the record with the max month and year. When I attempt to retrieve the record using this query

I get this results

enter image description here

It appears to me that it is separating the max year and max month independently of the other columns. The expected result should be enter image description here

how can I get down to the appropriate results? I have tried several self-joins to no avail.

Advertisement

Answer

You want to use Oracle’s built-in LAST (or FIRST) with MAX like this:

It will find the max of month in latest year only.

Advertisement