how to handle null dates with min() and max()?

LabKey Support Forum (Inactive)
how to handle null dates with min() and max()? Ben Bimber  2010-10-04 05:41
Status: Closed
 
we have a number of datasets that involve date ranges. you have participantId, startDate and endDate. a null end date means that this date range has not ended.

there's a number of instances where we group records to do things like 'find the last date when the animal had a roommate', 'last co-assignment', etc. both min(endDate) and max(endDate) will never report the null value. these functions return either the highest or lowest non-null value. null is completely ignored.

at least for this particular application, it would be nice if null were treated as the max() of a set of dates. it already sorts that way. unfortunately it seems that this is just how postgres handles dates. i ended up doing max(coalesce(endDate, now()) in other to handle the null values. it works, but is not great. is there a better way? thanks for the help.
 
 
jeckels responded:  2010-10-04 14:17
Hi Ben,

Your current approach seems like a good solution to me, all things considered. Since the SQL is relatively concise, we're probably best off prioritizing things like special date functions for overlap type queries that end up being very verbose, especially when the handle null.

Thanks,
Josh