From: Tom Lane <tgl@sss.pgh.pa.us>
To: Alban Hertroys <haramrae@gmail.com>
Cc: Postgres General <pgsql-general@postgresql.org>
Subject: Re: How to enumerate partitions from a window function?
Date: Thu, 03 Sep 2020 10:01:22 -0400
Message-ID: <583850.1599141682@sss.pgh.pa.us> (raw)
In-Reply-To: <CAF-3MvNs4k4ENssLo3MkrX2S8Z88FcK03Wr_myYounhkWiVj7w@mail.gmail.com>
References: <CAF-3MvNs4k4ENssLo3MkrX2S8Z88FcK03Wr_myYounhkWiVj7w@mail.gmail.com>
Alban Hertroys <haramrae@gmail.com> writes:
> As stated above, I want to enumerate the runs, starting at 1 and
> incrementing by 1 every time a partition from the 'run' window closes,
> Is there a way to achieve this through window functions, or do we need to
> wrap the thing in a subquery to achieve this?
I think this'll work:
select datetime, property_A, property_B
, first_value(datetime)::time over run as swap_time
, dense_rank() over (order by property_A, property_B)
, value
from process_data
window run as (partition by property_A, property_B order by datetime)
;
You can't do it with a window function over the "run" window because
no window function ever looks outside the current partition. But
that's easy to fix by using a different window definition. The
planner is smart enough to see that these windows are compatible
and only need one sort to be performed.
regards, tom lane
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, haramrae@gmail.com
Subject: Re: How to enumerate partitions from a window function?
In-Reply-To: <583850.1599141682@sss.pgh.pa.us>
References: <CAF-3MvNs4k4ENssLo3MkrX2S8Z88FcK03Wr_myYounhkWiVj7w@mail.gmail.com> <583850.1599141682@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