Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nxV6u-000224-C5 for pgsql-general@arkaria.postgresql.org; Sat, 04 Jun 2022 14:50:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nxV6s-0001iD-CJ for pgsql-general@arkaria.postgresql.org; Sat, 04 Jun 2022 14:50:38 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nxV6s-0001i3-1W for pgsql-general@lists.postgresql.org; Sat, 04 Jun 2022 14:50:38 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nxV6p-0000EI-Oz for pgsql-general@lists.postgresql.org; Sat, 04 Jun 2022 14:50:37 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 254EoVMH490059; Sat, 4 Jun 2022 10:50:31 -0400 From: Tom Lane To: Robert Stanford cc: "pgsql-generallists.postgresql.org" Subject: Re: Window function? In-reply-to: References: Comments: In-reply-to Robert Stanford message dated "Sat, 04 Jun 2022 18:18:30 +1000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <490057.1654354231.1@sss.pgh.pa.us> Date: Sat, 04 Jun 2022 10:50:31 -0400 Message-ID: <490058.1654354231@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Robert Stanford writes: > Can anyone suggest an elegant way to coalesce consecutive rows so only the > first start time and last end time for each group of events (by input) is > returned. Window functions don't change the number of query result rows, so that idea is a dead end. The standard way to solve this is GROUP BY: SELECT Input, MIN(Start) AS Start, MAX(End) AS End FROM ... GROUP BY Input ORDER BY Input -- optional If MIN/MAX don't quite express what you need to happen, then you might need to write custom aggregates. Point though is that you want aggregation within GROUP BY groups, not windowing. regards, tom lane