Re: Strange sequences - how to construct?

From: TIM CHILD <tim(dot)child(at)comcast(dot)net>
To: SQL Padawan <sql_padawan(at)protonmail(dot)com>, "pgsql-novice(at)lists(dot)postgresql(dot)org" <pgsql-novice(at)lists(dot)postgresql(dot)org>
Subject: Re: Strange sequences - how to construct?
Date: 2021-10-23 13:09:25
Message-ID: 1340188717.307707.1634994565348@connect.xfinity.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-novice

Here is a way using multiple sequences:

drop sequence if exists controller_sequence;
drop sequence if exists odd_values;
drop sequence if exists even_values;
create sequence if not exists controller_sequence;
create sequence if not exists odd_values start with 1;
create sequence if not exists even_values start with 1;
create function next_my_sequence() returns integer as
$body$
declare
choose integer;
begin
choose = nextval('controller_sequence');
if choose % 2 equals then
return nextval('even_values');
else
return nextval('odd_values');
end if;
end;
$body$
language plpgsql;
-- example: get 5 sequences
select next_my_sequence(), next_my_sequence(), next_my_sequence(), next_my_sequence(), next_my_sequence();

> On 10/22/2021 12:29 PM SQL Padawan <sql_padawan(at)protonmail(dot)com> wrote:
>
>
>
> Good afternoon to everybody.
>
> I wish to construct some weird sequences.
>
> 1
> 1
> 2
> 2
> &c.
>
> and with 3 ones, 4 ones... &c.
>
> Now, I know how to do a simple
> 1
> 2
> 3
> 4
>
> using both GENERATE_SERIES and using a RECURSIVE CTE.
>
> What I would like is to be able to construct my specified sequences using *_both_* GENERATE_SERIES *_and_* RECURSIVE CTEs.
>
> Regards,
>
> SQL Padawan!
>
>
>
>
>
> Sent with ProtonMail https://protonmail.com/ Secure Email.
>
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Laura Smith 2021-10-23 13:37:29 Re: ZFS filesystem - supported ?
Previous Message Mladen Gogala 2021-10-23 13:03:55 Re: ZFS filesystem - supported ?

Browse pgsql-novice by date

  From Date Subject
Next Message Alexey M Boltenkov 2021-10-23 13:54:07 Re: Strange sequences - how to construct?
Previous Message TIM CHILD 2021-10-22 23:01:14 Re: Strange sequences - how to construct?