Re: Window function?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Robert Stanford <rstanford(at)gmail(dot)com>
Cc: "pgsql-generallists(dot)postgresql(dot)org" <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: Window function?
Date: 2022-06-04 14:50:31
Message-ID: 490058.1654354231@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Robert Stanford <rstanford(at)gmail(dot)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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Jeff Janes 2022-06-04 16:00:12 Re: Why password authentication failed for user "postgres"?
Previous Message Robert Stanford 2022-06-04 13:18:06 Re: Window function?