I want a where-clause that returns always one row.
If for a query that returns multiple rows one of the values in a specific (nullable DateTime) field is NULL, it should return this row.
If there is no row with a value of NULL, it should return the row with the max-datetime.
For example:
Id | Date |
---|---|
1 | 2022-01-01 |
2 | NULL |
3 | 2021-01-01 |
In this example, the row with ID=2 should be returned.
Id | Date |
---|---|
1 | 2022-01-01 |
2 | 2020-01-01 |
3 | 2021-01-01 |
And in this example with no NULL row, the ID=1 row should be returned (because it has the highest date).
How is something like this possible?
Thanks in advance