From: Rob Sargent <robjsargent@gmail.com>
To: David G. Johnston <david.g.johnston@gmail.com>
Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: nextval per counted
Date: Fri, 27 Jan 2023 14:25:32 -0700
Message-ID: <68161326-65af-ee8b-0c9e-be5d8f0b4f7b@gmail.com> (raw)
In-Reply-To: <CAKFQuwaUV3L95TzJL8rtsvcNg24ZOayMOezFJME5Gdygfxi5Zg@mail.gmail.com>
References: <71aa4ae1-f789-485a-ae58-2acec9aa2df0@gmail.com>
<CAKFQuwaUV3L95TzJL8rtsvcNg24ZOayMOezFJME5Gdygfxi5Zg@mail.gmail.com>
On 1/27/23 14:20, David G. Johnston wrote:
> On Fri, Jan 27, 2023 at 1:59 PM Rob Sargent <robjsargent@gmail.com> wrote:
>
> I'm trying to craft SQL to invoke a sequence nextval once per
> grouped value.
>
>
> This seems like a very unusual usage of nextval/sequences...
>
> with cleanup as (
> select DISTINCT e.ma <http://e.ma;, coalesce(e.pa <http://e.pa;,
> 'fix') as pa from ...
> ), compute as (
> select ma, pa, nextval(...) from cleanup
> )
> select * from compute ... -- do whatever else you want
>
> So far I have this:
>
> with husb as(
> select e.ma <http://e.ma;, count(distinct coalesce(e.pa
> <http://e.pa;, nextval('egogen')::text)) as mates
> from emp_all_by3 e group by e.ma <http://e.ma; order by mates
> )
> select mates, count(*)
> from husb
> group by mates order by mates desc;
>
> Your "order by mates" in the CTE is totally pointless and wasting
> resources.
>
> David J.
>
Yeah, it wasn't when that was the last bit executed...
Seems asking a sequence for an id isn't too unusual? Or are they
specifically intended for primary keys?
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: robjsargent@gmail.com, david.g.johnston@gmail.com
Subject: Re: nextval per counted
In-Reply-To: <68161326-65af-ee8b-0c9e-be5d8f0b4f7b@gmail.com>
References: <71aa4ae1-f789-485a-ae58-2acec9aa2df0@gmail.com> <CAKFQuwaUV3L95TzJL8rtsvcNg24ZOayMOezFJME5Gdygfxi5Zg@mail.gmail.com> <68161326-65af-ee8b-0c9e-be5d8f0b4f7b@gmail.com>
* 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