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 1s9mkE-0058Nl-6w for pgsql-general@arkaria.postgresql.org; Wed, 22 May 2024 14:15:07 +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 1s9mkE-00HMwP-93 for pgsql-general@arkaria.postgresql.org; Wed, 22 May 2024 14:15:06 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1s9mkD-00HMwE-Ub for pgsql-general@lists.postgresql.org; Wed, 22 May 2024 14:15:05 +0000 Received: from mail-lf1-x129.google.com ([2a00:1450:4864:20::129]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1s9mkB-001Shb-EM for pgsql-general@lists.postgresql.org; Wed, 22 May 2024 14:15:04 +0000 Received: by mail-lf1-x129.google.com with SMTP id 2adb3069b0e04-51f0602bc58so1054151e87.0 for ; Wed, 22 May 2024 07:15:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716387302; x=1716992102; 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=+MiS7Ym4RgyHwLOlpgrcEpS+XHFUDzqAiRQqq0tweoY=; b=BHDNtm2ouOb4gqEuFqu6ls/zX7vacwUnjwG8oGjfNxQoIF+Ek4u0igA8MJFAoWsIGg VfI7omgUJUcxFIsex5THz5D9rdaM1EzUVmd6cmyD52V03oEtAB9uc0XDxDu1zI3RmCTF fuDrvXeKXBQIgjasPY+5zAeCaukfxEIeOISKIWMS9ZFAgFpu2/k7i503qHasZls6t9fE OQliQfBq1WhVtJ2RWbmp6u8ANKHjYascJYfXBpOqfaMvf/5B0jH00r/G2OkknHpqJ4n/ cowPNVirXpLkz1+t4AOOtBBOSCzdQKD16eTUCAD3iAtOW6PotB7/fFwBJK7wixRPyjMG VEVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716387302; x=1716992102; 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=+MiS7Ym4RgyHwLOlpgrcEpS+XHFUDzqAiRQqq0tweoY=; b=RkZTIjnC3E7Eph07aijlwJQ/IQtXpTVA44zkfkVXDyWDc8kiXyx4R6R/n3dr43C57K JttKt6Z89G/D6xOFXBuRIT2IJltrtKBkAmwrtfc5I5Tf1oyWa7bSDpu+xqy0RbmWso5P eWhkMMead500ya7OsJ1iOvVtlMdbRxoEHVJSZzgrO/z3ctB7mCjynvM1eBgRXFtuku/m s8LKBBCZWRFRQArNcjtB3bgzjNNrvZMFRbgHJJuMB88Q6OGPzo6OEgGAVtNmcJ7Owuv5 6KRJ/MvdsyD2stmNOBzT5sqBXm/FG2OH0Y6YtSB0H54Wxn7ZFg75puo/77L2/Aop+Mtz Tq4Q== X-Gm-Message-State: AOJu0YyWTfETsdYWZpYWh9pkTb7kb47zAjlLEniq+8IQk6jTymmltU+H nJrMAl7a4uUVjMkqwu4T3eltPYYHH/loanq6cYLzasegQsjoYCstEwm38MhiMXQ3oggdohMpOYx zcxo0JJ+ZLlEkDAgwH54isUOQ+flNSCsq X-Google-Smtp-Source: AGHT+IFq50IMS+lhnOazGjpY0xAvK9aTIIwaLgFfLnhfcg/JlN/cEwRvpeq+oFXWlgGJbdbenUsX6ddocbMrk2WTWHg= X-Received: by 2002:ac2:47e9:0:b0:51f:2a80:24d3 with SMTP id 2adb3069b0e04-52407ac10f6mr3550184e87.19.1716387301571; Wed, 22 May 2024 07:15:01 -0700 (PDT) MIME-Version: 1.0 References: <98fd5198e1fe4e162f1166fd0575844de8bb7714.camel@loonybin.net> In-Reply-To: <98fd5198e1fe4e162f1166fd0575844de8bb7714.camel@loonybin.net> From: Greg Sabino Mullane Date: Wed, 22 May 2024 10:14:25 -0400 Message-ID: Subject: Re: Finding "most recent" using daterange To: Rob Foehl Cc: pgsql-general@lists.postgresql.org Content-Type: multipart/alternative; boundary="00000000000068453406190b8f11" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --00000000000068453406190b8f11 Content-Type: text/plain; charset="UTF-8" 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 --00000000000068453406190b8f11 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
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 *, =C2=A0row= _number() over (partition by id order by upper(dates) desc, lower(dates) de= sc) from example)
=C2=A0 select id,value,dates from x where row_n= umber =3D 1;

=C2=A0id | value | =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0dates
----+-------+-------------------------
=C2= =A0 1 | b =C2=A0 =C2=A0 | [2010-01-01,)
=C2=A0 2 | d =C2=A0 =C2=A0 | [20= 10-01-01,2021-01-01)
=C2=A0 3 | g =C2=A0 =C2=A0 | [2013-01-01,)
=C2= =A0 4 | j =C2=A0 =C2=A0 | [2010-01-01,2015-01-01)
(4 rows)


Cheers,
Greg

<= /div> --00000000000068453406190b8f11--