From: TIM CHILD <tim.child@comcast.net>
To: SQL Padawan <sql_padawan@protonmail.com>
To: pgsql-novice@lists.postgresql.org <pgsql-novice@lists.postgresql.org>
Subject: Re: Strange sequences - how to construct?
Date: Sat, 23 Oct 2021 06:09:25 -0700 (PDT)
Message-ID: <1340188717.307707.1634994565348@connect.xfinity.com> (raw)
In-Reply-To: <tiUZZ67DYCE5aY4wDAfIXjzlghyf4ndcG2m7wWRd0Rq3rtyhd38iJ9fN2iG088MxWOQtEBlEbsLXzqq-RUYer6rXXfS67pjdwthg8YRIgQU=@protonmail.com>
References: <tiUZZ67DYCE5aY4wDAfIXjzlghyf4ndcG2m7wWRd0Rq3rtyhd38iJ9fN2iG088MxWOQtEBlEbsLXzqq-RUYer6rXXfS67pjdwthg8YRIgQU=@protonmail.com>
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@protonmail.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.
>
>
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: tim.child@comcast.net, sql_padawan@protonmail.com, pgsql-novice@lists.postgresql.org
Subject: Re: Strange sequences - how to construct?
In-Reply-To: <1340188717.307707.1634994565348@connect.xfinity.com>
References: <tiUZZ67DYCE5aY4wDAfIXjzlghyf4ndcG2m7wWRd0Rq3rtyhd38iJ9fN2iG088MxWOQtEBlEbsLXzqq-RUYer6rXXfS67pjdwthg8YRIgQU=@protonmail.com> <1340188717.307707.1634994565348@connect.xfinity.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