Re: nextval per counted

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Rob Sargent <robjsargent(at)gmail(dot)com>
Cc: "pgsql-generallists(dot)postgresql(dot)org" <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: nextval per counted
Date: 2023-01-27 21:20:55
Message-ID: CAKFQuwaUV3L95TzJL8rtsvcNg24ZOayMOezFJME5Gdygfxi5Zg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, Jan 27, 2023 at 1:59 PM Rob Sargent <robjsargent(at)gmail(dot)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, coalesce(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, count(distinct coalesce(e.pa, nextval('egogen')::text)) as
> mates
> from emp_all_by3 e group by 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.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Rob Sargent 2023-01-27 21:25:32 Re: nextval per counted
Previous Message Tom Lane 2023-01-27 21:16:24 Re: Indexes mysteriously change to ON ONLY