pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedWindow function?
6+ messages / 5 participants
[nested] [flat]
* Window function?
@ 2022-06-04 08:18 Robert Stanford <rstanford@gmail.com>
0 siblings, 3 replies; 6+ messages in thread
From: Robert Stanford @ 2022-06-04 08:18 UTC (permalink / raw)
To: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
Hi,
I have time series data from multiple inputs with start and
end timestamps.
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.
Sample from and to below where the rows for Input number 4 could be
massaged.
(Timestamps selected with timestamp(0) for convenience)
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Window function?
@ 2022-06-04 12:56 Thiemo Kellner <thiemo@gelassene-pferde.biz>
parent: Robert Stanford <rstanford@gmail.com>
2 siblings, 1 reply; 6+ messages in thread
From: Thiemo Kellner @ 2022-06-04 12:56 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
Hi Robert
Interesting problem. I need to think about it.
You need to figure out when Input changes. You can achieve this by using
lead or lag (depending of the sort direction over start)
https://www.postgresql.org/docs/current/functions-window.html .
Hope this nudges you to a solution.
Kind regards
Thiemo
Am 04.06.22 um 10:18 schrieb Robert Stanford:
> Hi,
>
> I have time series data from multiple inputs with start and
> end timestamps.
>
> 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.
>
> Sample from and to below where the rows for Input number 4 could be
> massaged.
> (Timestamps selected with timestamp(0) for convenience)
>
> From this:
> Input Start End
> 5 2022-06-04 09:09:00 2022-06-04 09:09:29
> 4 2022-06-04 09:08:50 2022-06-04 09:09:00
> 4 2022-06-04 09:08:10 2022-06-04 09:08:50
> 4 2022-06-04 09:07:47 2022-06-04 09:08:10
> 17 2022-06-04 09:06:47 2022-06-04 09:07:47
> 4 2022-06-04 09:06:37 2022-06-04 09:06:47
> 4 2022-06-04 09:06:29 2022-06-04 09:06:37
> 4 2022-06-04 09:06:17 2022-06-04 09:06:29
> 4 2022-06-04 09:05:53 2022-06-04 09:06:17
> 16 2022-06-04 09:04:33 2022-06-04 09:05:53
>
> To this:
> Input Start End
> 5 2022-06-04 09:09:00 2022-06-04 09:09:29
> 4 2022-06-04 09:07:47 2022-06-04 09:09:00
> 17 2022-06-04 09:06:47 2022-06-04 09:07:47
> 4 2022-06-04 09:05:53 2022-06-04 09:06:47
> 16 2022-06-04 09:04:33 2022-06-04 09:05:53
>
> Thanks in advance to anyone who can help!
> Robert
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Window function?
@ 2022-06-04 13:18 Robert Stanford <rstanford@gmail.com>
parent: Thiemo Kellner <thiemo@gelassene-pferde.biz>
0 siblings, 0 replies; 6+ messages in thread
From: Robert Stanford @ 2022-06-04 13:18 UTC (permalink / raw)
To: Thiemo Kellner <thiemo@gelassene-pferde.biz>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
On Sat, 4 Jun 2022 at 22:56, Thiemo Kellner <thiemo@gelassene-pferde.biz>
wrote:
> Hi Robert
>
> Interesting problem. I need to think about it.
>
> You need to figure out when Input changes. You can achieve this by using
> lead or lag (depending of the sort direction over start)
> https://www.postgresql.org/docs/current/functions-window.html .
>
After a bit of poking around this seems to get things going in the right
direction.
select row_number() over (order by start) - row_number() over (partition
by input order by start)
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Window function?
@ 2022-06-04 14:50 Tom Lane <tgl@sss.pgh.pa.us>
parent: Robert Stanford <rstanford@gmail.com>
2 siblings, 1 reply; 6+ messages in thread
From: Tom Lane @ 2022-06-04 14:50 UTC (permalink / raw)
To: Robert Stanford <rstanford@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
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
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Window function?
@ 2022-06-04 16:25 David G. Johnston <david.g.johnston@gmail.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 6+ messages in thread
From: David G. Johnston @ 2022-06-04 16:25 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Robert Stanford <rstanford@gmail.com>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
On Sat, Jun 4, 2022 at 7:50 AM Tom Lane <tgl@sss.pgh.pa.us> wrote:
> 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
>
Assuming that input group 4 really is supposed to show up twice in the
output then the final solution is going to have to be a combination of this
and window functions. You need the later in order to be able to say "input
4, appearance 1" and "input 4, appearance 2" - which lead/lag tends to help
with. Then the group by becomes {input, appearance}.
David J.
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Window function?
@ 2022-06-04 17:06 John W Higgins <wishdev@gmail.com>
parent: Robert Stanford <rstanford@gmail.com>
2 siblings, 0 replies; 6+ messages in thread
From: John W Higgins @ 2022-06-04 17:06 UTC (permalink / raw)
To: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
On Sat, Jun 4, 2022 at 1:18 AM Robert Stanford <rstanford@gmail.com> wrote:
> Hi,
>
> From this:
> Input Start End
> 5 2022-06-04 09:09:00 2022-06-04 09:09:29
> 4 2022-06-04 09:08:50 2022-06-04 09:09:00
> 4 2022-06-04 09:08:10 2022-06-04 09:08:50
> 4 2022-06-04 09:07:47 2022-06-04 09:08:10
> 17 2022-06-04 09:06:47 2022-06-04 09:07:47
> 4 2022-06-04 09:06:37 2022-06-04 09:06:47
> 4 2022-06-04 09:06:29 2022-06-04 09:06:37
> 4 2022-06-04 09:06:17 2022-06-04 09:06:29
> 4 2022-06-04 09:05:53 2022-06-04 09:06:17
> 16 2022-06-04 09:04:33 2022-06-04 09:05:53
>
> To this:
> Input Start End
> 5 2022-06-04 09:09:00 2022-06-04 09:09:29
> 4 2022-06-04 09:07:47 2022-06-04 09:09:00
> 17 2022-06-04 09:06:47 2022-06-04 09:07:47
> 4 2022-06-04 09:05:53 2022-06-04 09:06:47
> 16 2022-06-04 09:04:33 2022-06-04 09:05:53
>
lag is indeed your friend here - assuming times is your table name
with times_cte as (select *, lag(input, 1) over () from times)
select input, start, end from times_cte where input != coalesce(lag, -1);
The coalesce to -1 is needed at the end to get the first row which has null
for the lag value because it's the first row.
John
^ permalink raw reply [nested|flat] 6+ messages in thread
end of thread, other threads:[~2022-06-04 17:06 UTC | newest]
Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2022-06-04 08:18 Window function? Robert Stanford <rstanford@gmail.com>
2022-06-04 12:56 ` Thiemo Kellner <thiemo@gelassene-pferde.biz>
2022-06-04 13:18 ` Robert Stanford <rstanford@gmail.com>
2022-06-04 14:50 ` Tom Lane <tgl@sss.pgh.pa.us>
2022-06-04 16:25 ` David G. Johnston <david.g.johnston@gmail.com>
2022-06-04 17:06 ` John W Higgins <wishdev@gmail.com>
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