pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Rob Sargent <robjsargent@gmail.com>
To: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: nextval per counted
Date: Fri, 27 Jan 2023 13:59:39 -0700
Message-ID: <71aa4ae1-f789-485a-ae58-2acec9aa2df0@gmail.com> (raw)

I'm trying to craft SQL to invoke a sequence nextval once per grouped value.

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;

which works nicely but it "ids" each null separately.

The following lets me count the "fixes" as a mate

    with husb as(
       select e.ma, count(distinct coalesce(e.pa,
    nextval('egogen')::text)) mates
       from emp_all_by3 e
       where ma is not null
       group by e.ma order by mates
    )
    select mates, count(*) from husb group by mates order by mates desc;

    with husb as(
       select e.ma, coalesce(e.pa,'fix') as pa
       from emp_all_by3 e
       where e.ma is not null
    ),
    fixed as (
       select e.ma, count(distinct e.pa) mates
       from husb e group by e.ma order by mates
    )
    select mates, count(*) from fixed group by mates order by mates desc;

but I would love to able to assign a single "nextval"  to those fixes.

Any pointers appreciated.

view thread (5+ messages)  latest in thread

Message-ID: <71aa4ae1-f789-485a-ae58-2acec9aa2df0@gmail.com>
Permalink:  ../71aa4ae1-f789-485a-ae58-2acec9aa2df0@gmail.com/
Also on:    postgresql.org/message-id/71aa4ae1-f789-485a-ae58-2acec9aa2df0@gmail.com

 · 

reply

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
  Subject: Re: nextval per counted
  In-Reply-To: <71aa4ae1-f789-485a-ae58-2acec9aa2df0@gmail.com>
  References: <71aa4ae1-f789-485a-ae58-2acec9aa2df0@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