Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1s9nek-005ELQ-0x for pgsql-general@arkaria.postgresql.org; Wed, 22 May 2024 15:13:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1s9nej-000Q0A-Uh for pgsql-general@arkaria.postgresql.org; Wed, 22 May 2024 15:13:29 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1s9nej-000Q00-Jy for pgsql-general@lists.postgresql.org; Wed, 22 May 2024 15:13:29 +0000 Received: from mail-pg1-x52c.google.com ([2607:f8b0:4864:20::52c]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1s9neg-000DkM-Ra for pgsql-general@lists.postgresql.org; Wed, 22 May 2024 15:13:28 +0000 Received: by mail-pg1-x52c.google.com with SMTP id 41be03b00d2f7-663d2e6a3d7so818799a12.0 for ; Wed, 22 May 2024 08:13:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716390805; x=1716995605; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=MrD9L6aIUVBkWa27YLRFglj/2nd75jQ+jeZdtRfWT8o=; b=ZgNGMzO1s9hNq6zBAsROGVpXUfsbJ9dXPtsS7Fn+avE0gO7dtEQfVeviuqVoLEADkq QGb4GaxXFnxZTuk4wj1iUdtTVkPwrgykTBHI2kzI0vqphPQJBQPY+Vorz23wUanrQ5l4 Wpy1mIqbsB0F78EJ7EjAeavszFTjyG+iLkBY3A6ggpgSdvTeoA1z/81u2m9SfL+9237L LdghNQjLDuaHK8odmnkDu/rYWAbp8/hxDJHOqPsd4ACEL2kgUEnMI6bg9YmJQV7g5z2q CeMe5voQRfRJqZ8Xu2AM6fFIjnu/5bKZ7a5jHdRmdAjOl7we+Gn9Kg+/kZ3rFgYegq0o Ig3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716390805; x=1716995605; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=MrD9L6aIUVBkWa27YLRFglj/2nd75jQ+jeZdtRfWT8o=; b=aYv5aguCPOt/6w4Z+qR3YOCicPhH2RH6o8f6VDmysmcQDA+tPep8EKfjdWcVN4g6a9 QNjt+DWAHSVZy/wszofU73lhNEcwC4UJcYbp6aO/7x53zyZnPual1rznZCAc4KKw9G4M damqrRcrqmTgkoRunYK26DEcgcyRIt0N7j91yGQMqBmqpSFMmTaDQlvJ82DrilNudaZy ARm+AW12dJJzqe7j6OoS0csBTHQBNBEQTZH4nnTUgDvXyoIWMQMyI4iXeveyNd2qP1YL o/ZwPZ+ZOhK7hI3Ea879oJeoJH7/FU5QVcjI33GeCtH46lojS9/rvQJzVjQuxzqDzSPR tWYQ== X-Forwarded-Encrypted: i=1; AJvYcCWNZU6UjYmoRn4pEkuEjN6rXmLfGR41tS51JcZMEbndi1U0s4RjqUCY9L2PozjwpiuQfkDSWOo+sxjR7GwjjRosBoxt2GZsJiSiiGLZmRt5RsQo X-Gm-Message-State: AOJu0Ywj2twXIMyyEJE6g2agwthTpCOiFe6uOXT6klnvD/z2UTpyYZcI oXlueNpuctFsgAFyPAVpG9ieR+9JOIMcx93rl6E+MK7vuGWrGM4d0JYH8GchIQdlzbNxYB8/E1V DOcBQHvmYqSEqwbZd+nT7LuyW31Y= X-Google-Smtp-Source: AGHT+IFb7SWxndNqLkwZETRvv+cZmy1ThqrvpaLOZZjEaGp7v6KmQ+haoKE0JOsMt7NogYoK24AesFRC2h+ZvQuQxmQ= X-Received: by 2002:a17:90a:8d81:b0:2b3:6898:d025 with SMTP id 98e67ed59e1d1-2bd9f48f0d2mr3359182a91.9.1716390804871; Wed, 22 May 2024 08:13:24 -0700 (PDT) MIME-Version: 1.0 References: <98fd5198e1fe4e162f1166fd0575844de8bb7714.camel@loonybin.net> In-Reply-To: From: Isaac Morland Date: Wed, 22 May 2024 11:13:12 -0400 Message-ID: Subject: Re: Finding "most recent" using daterange To: Greg Sabino Mullane Cc: Rob Foehl , pgsql-general@lists.postgresql.org Content-Type: multipart/alternative; boundary="0000000000003861d206190c6061" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000003861d206190c6061 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, 22 May 2024 at 10:15, Greg Sabino Mullane 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 abou= t > 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 =3D 1; > Don=E2=80=99t you need NULLS LAST for the lower bounds? There NULL means so= mething 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. --0000000000003861d206190c6061 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Wed, 22 May 2024 at 10:15, Greg Sabino= Mullane <htamfids@gmail.com&g= t; wrote:
This is a good candidate for a window function. Also note that nulls a= lready get sorted correctly by the DESC so no need to get 'infinity'= ; involved, although you could write 'DESC NULLS FIRST' to be expli= cit about it.

with x as (select *, =C2=A0row_number() ov= er (partition by id order by upper(dates) desc, lower(dates) desc) from exa= mple)
=C2=A0 select id,value,dates from x where row_number =3D 1;=

Don=E2=80=99t you need NULLS L= AST for the lower bounds? There NULL means something closer to -infinity an= d should appear after the=C2=A0non-NULL values in a descending sort.
<= div>
Actually it strikes me that this sorting issue could be = a reason to avoid NULL bounds on ranges and prefer the use of=C2=A0+/-infin= ity if the underlying data type supports it.
--0000000000003861d206190c6061--