Re: Finding "most recent" using daterange

From: Isaac Morland <isaac(dot)morland(at)gmail(dot)com>
To: Greg Sabino Mullane <htamfids(at)gmail(dot)com>
Cc: Rob Foehl <rwf(at)loonybin(dot)net>, pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: Finding "most recent" using daterange
Date: 2024-05-22 15:13:12
Message-ID: CAMsGm5e5=YcjaUGQogkxMqrj9_beuSmVOrXTAuQPsBdmkbZPSA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, 22 May 2024 at 10:15, Greg Sabino Mullane <htamfids(at)gmail(dot)com>
wrote:

> This is a good candidate for a window function. Also note that nulls
> already get sorted correctly by the DESC so no need to get 'infinity'
> involved, although you could write 'DESC NULLS FIRST' to be explicit about
> it.
>
> with x as (select *, row_number() over (partition by id order by
> upper(dates) desc, lower(dates) desc) from example)
> select id,value,dates from x where row_number = 1;
>

Don’t you need NULLS LAST for the lower bounds? There NULL means something
closer to -infinity and should appear after the non-NULL values in a
descending sort.

Actually it strikes me that this sorting issue could be a reason to avoid
NULL bounds on ranges and prefer the use of +/-infinity if the underlying
data type supports it.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Greg Sabino Mullane 2024-05-22 15:35:58 Re: Finding "most recent" using daterange
Previous Message Adrian Klaver 2024-05-22 14:48:04 Re: search_path and SET ROLE