Skip to content
Advertisement

Selecting the maximum date before a certain year

I have a table where I’d like to select only the record with the maximum date:

The issue is that I have some IDs in the table where the maximum date is in 2022, so that is what gets selected. How do I select the maximum date for a record that is before 01JAN2022 so that my output look like this:

Advertisement

Answer

There are multiple ways to achieve this. The simplest is probably to sort your records by descending date and fetch only the first one. If you don’t want rows with a certain condition, such as dates after 01JAN2022, just add a WHERE.

You didn’t indicate a specific RDBMS so your exact syntax may vary a bit.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement