| From: | Rob Sargent <robjsargent(at)gmail(dot)com> |
|---|---|
| To: | "pgsql-generallists(dot)postgresql(dot)org" <pgsql-general(at)lists(dot)postgresql(dot)org> |
| Subject: | nextval per counted |
| Date: | 2023-01-27 20:59:39 |
| Message-ID: | [email protected] |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-general |
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.
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Rumpi Gravenstein | 2023-01-27 21:01:31 | Re: Indexes mysteriously change to ON ONLY |
| Previous Message | Tom Lane | 2023-01-27 20:53:34 | Re: Indexes mysteriously change to ON ONLY |