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 1s9qNL-005Tj9-8H for pgsql-general@arkaria.postgresql.org; Wed, 22 May 2024 18:07:44 +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 1s9qNL-002HFL-16 for pgsql-general@arkaria.postgresql.org; Wed, 22 May 2024 18:07:43 +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 1s9qNK-002HEM-Lq for pgsql-general@lists.postgresql.org; Wed, 22 May 2024 18:07:42 +0000 Received: from mail-ej1-x62e.google.com ([2a00:1450:4864:20::62e]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1s9qNF-000EuV-7I for pgsql-general@lists.postgresql.org; Wed, 22 May 2024 18:07:42 +0000 Received: by mail-ej1-x62e.google.com with SMTP id a640c23a62f3a-a59b49162aeso1002749566b.3 for ; Wed, 22 May 2024 11:07:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716401256; x=1717006056; darn=lists.postgresql.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=i+wGFbYkzmKpc9feL5/geHw2OndNtgOLVzib13po0Ms=; b=mvnD4FueD4NuKvABgVqm0p070s9xQZDtSrZ8i65k2LviCJD5dFh0SjP+zF+I+KWCAw 7rhsGKjeeqJLGkJqD/Ok+Y8uujjFVLDHgouBczXVxAnKuA59vDrnZbx+hba/KR2sGwzu lVHLeaOD3saB8hIWz6El39etlyP53m2vuuUipmvMFxt8z87fx6rZZbZWfViNmFI4NAVP LMSuMh3ExZW/pooYSb01SxqvJ9LyMaaWdrQ5osVRThl6MKb640JOlw/TTzhKrjn50wKE q4SYnG9OJ6u9CdE6YfNtfMNCin8sbz+FUohjv2BZQIY3ajJiFSi6LWuqulmSHZkYygux NuXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716401256; x=1717006056; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=i+wGFbYkzmKpc9feL5/geHw2OndNtgOLVzib13po0Ms=; b=LWIjPYLNt+XOnLOoTQpizKpFoII37arBLDDccOyD9JpvJ0/EdSHtTFkvTccbF0rZDX 8cTDKo/mk+/vSClFBS4Wink65mi3PtjUOvhpto9P7WJIOEXN+k1PUVF/PfIWEq9BbqsD Gya3p6PMhJbLAljDI6m+TR7FviPw59eNFJ/b51a3afCq4RPxQBfqh1PKZR9mbf3A5bdI uDTD+pVoGxVVflq6IrVH93f/7GVsj5P0pIupaxuS/sOW5a16VvHadmUODCfoH/NDU+/U ku/pdk73WWXOngZal+cMRjFkhoZmc+e/iStp+0ol6G/7R6Xtea0YmjbhPANGP+d6B0yQ Vfig== X-Gm-Message-State: AOJu0YzHN+C9xWkvhJUExbJJauRAYDFFGp4OzQVlPQIJrExtkMaUfGyJ rUk4Nht/INmAzmkJWIUEUZFbucXUCKCX3TQnk/rtmSyxwshSOUQG X-Google-Smtp-Source: AGHT+IG/wj74j18CxfXHVET0lo0b5eumvL1Ftc6tVUFeGgBcBIwbgAQVdTl2manh5HiJKbqYViWugA== X-Received: by 2002:a17:906:174d:b0:a59:9b8e:aa61 with SMTP id a640c23a62f3a-a62280a020amr193514366b.35.1716401256314; Wed, 22 May 2024 11:07:36 -0700 (PDT) Received: from smtpclient.apple ([188.212.112.125]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a5cedda22acsm881903466b.137.2024.05.22.11.07.35 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 22 May 2024 11:07:35 -0700 (PDT) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3774.600.62\)) Subject: Re: Finding "most recent" using daterange From: Alban Hertroys In-Reply-To: <98fd5198e1fe4e162f1166fd0575844de8bb7714.camel@loonybin.net> Date: Wed, 22 May 2024 20:07:25 +0200 Cc: pgsql-general Content-Transfer-Encoding: quoted-printable Message-Id: References: <98fd5198e1fe4e162f1166fd0575844de8bb7714.camel@loonybin.net> To: Rob Foehl X-Mailer: Apple Mail (2.3774.600.62) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On 22 May 2024, at 09:58, Rob Foehl wrote: >=20 > Coming back to PostgreSQL after a (decades-)long absence... If I have > something like: >=20 > CREATE TABLE example ( > id integer NOT NULL, > value text NOT NULL, > dates daterange NOT NULL > ); >=20 > INSERT INTO example VALUES > (1, 'a', '[2010-01-01,2020-01-01)'), > (1, 'b', '[2010-01-01,)'), > (1, 'c', '[,2021-01-01)'), > (2, 'd', '[2010-01-01,2021-01-01)'), > (2, 'e', '[2015-01-01,2020-01-01)'), > (3, 'f', '[2014-01-01,2016-01-01)'), > (3, 'g', '[2013-01-01,)'), > (3, 'h', '[2012-01-01,)'), > (3, 'i', '[2013-01-01,2017-01-01)'), > (4, 'j', '[2010-01-01,2015-01-01)'); >=20 > and I want to find the "most recent" value out of each group, meaning > that having the greatest upper bound followed by the greatest lower > bound, what I've managed to come up with thus far is: Sounds like a good candidate for using EXISTS to prove that no more = recent value exists for a given id: SELECT e.id, e.value, e.dates FROM example AS e WHERE NOT EXISTS ( SELECT 1 FROM example AS i WHERE i.id =3D e.id AND (coalesce(upper(i.dates), 'infinity') > = coalesce(upper(e.dates), 'infinity') OR (coalesce(upper(i.dates), 'infinity') =3D = coalesce(upper(e.dates), 'infinity') AND coalesce(lower(i.dates), '-infinity') > = coalesce(lower(e.dates), '-infinity')) ) ); id | value | dates =20 ----+-------+------------------------- 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) Alban Hertroys -- If you can't see the forest for the trees, cut the trees and you'll find there is no forest.