public inbox for [email protected]  
help / color / mirror / Atom feed
From: Greg Sabino Mullane <[email protected]>
To: Rob Foehl <[email protected]>
Cc: [email protected]
Subject: Re: Finding "most recent" using daterange
Date: Wed, 22 May 2024 10:14:25 -0400
Message-ID: <CAKAnmmLBxrQ0n4+BPb_xBQKrDRoiq4eSKdQYDbnmo7KTQVFuRg@mail.gmail.com> (raw)
In-Reply-To: <[email protected]>
References: <[email protected]>

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;

 id | value |          dates
----+-------+-------------------------
  1 | b     | [2010-01-01,)
  2 | d     | [2010-01-01,2021-01-01)
  3 | g     | [2013-01-01,)
  4 | j     | [2010-01-01,2015-01-01)
(4 rows)


Cheers,
Greg


view thread (4+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected], [email protected]
  Subject: Re: Finding "most recent" using daterange
  In-Reply-To: <CAKAnmmLBxrQ0n4+BPb_xBQKrDRoiq4eSKdQYDbnmo7KTQVFuRg@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox