pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Robert Stanford <rstanford@gmail.com>
Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: Window function?
Date: Sat, 04 Jun 2022 10:50:31 -0400
Message-ID: <490058.1654354231@sss.pgh.pa.us> (raw)
In-Reply-To: <CAC1FqCEFtphM0EVZY01TA5TkSn8v8YdQRpXgasfmVYPtK-EsRw@mail.gmail.com>
References: <CAC1FqCEFtphM0EVZY01TA5TkSn8v8YdQRpXgasfmVYPtK-EsRw@mail.gmail.com>

Robert Stanford <rstanford@gmail.com> 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





view thread (6+ messages)  latest in thread

Message-ID: <490058.1654354231@sss.pgh.pa.us>
Permalink:  ../490058.1654354231@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/490058.1654354231@sss.pgh.pa.us

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: tgl@sss.pgh.pa.us, rstanford@gmail.com
  Subject: Re: Window function?
  In-Reply-To: <490058.1654354231@sss.pgh.pa.us>
  References: <CAC1FqCEFtphM0EVZY01TA5TkSn8v8YdQRpXgasfmVYPtK-EsRw@mail.gmail.com> <490058.1654354231@sss.pgh.pa.us>

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

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