Skip to content
Advertisement

SQL find max date based on a non null other column

I have a table like so:

And I would like to return an entire row containing the uniqueID, scandatetime, and latest scanfacilityname (i.e., max scandatetime where scanfacilityname is not null). I have tried the following query:

But im not sure if this is correct/if I need the coalesce.

Advertisement

Answer

You can use the max_by function:

See the doc.

There’s no coalesce needed since the max and max_by functions will effectively ignore null values.

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