pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feednextval per counted
5+ messages / 2 participants
[nested] [flat]
* nextval per counted
@ 2023-01-27 20:59 Rob Sargent <robjsargent@gmail.com>
0 siblings, 1 reply; 5+ messages in thread
From: Rob Sargent @ 2023-01-27 20:59 UTC (permalink / raw)
To: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
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.
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: nextval per counted
@ 2023-01-27 21:20 David G. Johnston <david.g.johnston@gmail.com>
parent: Rob Sargent <robjsargent@gmail.com>
0 siblings, 1 reply; 5+ messages in thread
From: David G. Johnston @ 2023-01-27 21:20 UTC (permalink / raw)
To: Rob Sargent <robjsargent@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
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, 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.
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: nextval per counted
@ 2023-01-27 21:25 Rob Sargent <robjsargent@gmail.com>
parent: David G. Johnston <david.g.johnston@gmail.com>
0 siblings, 1 reply; 5+ messages in thread
From: Rob Sargent @ 2023-01-27 21:25 UTC (permalink / raw)
To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
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?
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: nextval per counted
@ 2023-01-27 21:31 David G. Johnston <david.g.johnston@gmail.com>
parent: Rob Sargent <robjsargent@gmail.com>
0 siblings, 1 reply; 5+ messages in thread
From: David G. Johnston @ 2023-01-27 21:31 UTC (permalink / raw)
To: Rob Sargent <robjsargent@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
On Fri, Jan 27, 2023 at 2:25 PM Rob Sargent <robjsargent@gmail.com> wrote:
> 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, 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.
>
> 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?
>
>
Yes, their design is excellent for surrogate primary keys. I don't even
know what to call what you are doing but it isn't that. It seems like some
form of counting but stuff like "row_number" and "count" perform that
function. I don't think I'd trust using them as a counter...for that I'd
do counting in the query then have a separate aspect, in user-space, not
the system catalogs, that increments a counter.
David J.
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: nextval per counted
@ 2023-01-27 21:41 Rob Sargent <robjsargent@gmail.com>
parent: David G. Johnston <david.g.johnston@gmail.com>
0 siblings, 0 replies; 5+ messages in thread
From: Rob Sargent @ 2023-01-27 21:41 UTC (permalink / raw)
To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
On 1/27/23 14:31, David G. Johnston wrote:
> On Fri, Jan 27, 2023 at 2:25 PM Rob Sargent <robjsargent@gmail.com> wrote:
>
> 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?
>
>
> Yes, their design is excellent for surrogate primary keys. I don't
> even know what to call what you are doing but it isn't that. It seems
> like some form of counting but stuff like "row_number" and "count"
> perform that function. I don't think I'd trust using them as a
> counter...for that I'd do counting in the query then have a separate
> aspect, in user-space, not the system catalogs, that increments a counter.
>
> David J.
>
Yeah, in this case I'm actually trying to assign an id in place of a
null, but only one id for all null for one ma. One thought was to
simply use the "ma" value but the table constraints are such that that
cannot be.
TL/DR
This is pedigree ego/ma/pa triplet work. Set of ego with a mother and
missing father can be considered a sibship (one mother/father pair) or a
"half-sibship" where each child get assigned a unique father (one
mother/N fathers). In that latter case, the sequence works perfectly.
We don't allow for mother/mother, the analysis tools are not up for that
(yet).
Thanks for your time,
rjs
^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2023-01-27 21:41 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2023-01-27 20:59 nextval per counted Rob Sargent <robjsargent@gmail.com>
2023-01-27 21:20 ` David G. Johnston <david.g.johnston@gmail.com>
2023-01-27 21:25 ` Rob Sargent <robjsargent@gmail.com>
2023-01-27 21:31 ` David G. Johnston <david.g.johnston@gmail.com>
2023-01-27 21:41 ` Rob Sargent <robjsargent@gmail.com>
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