Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1k1wgA-0003YE-BU for pgsql-sql@arkaria.postgresql.org; Sat, 01 Aug 2020 18:56:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1k1wg8-0001Sj-38 for pgsql-sql@arkaria.postgresql.org; Sat, 01 Aug 2020 18:56:20 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1k1wg7-0001Sb-Rr for pgsql-sql@lists.postgresql.org; Sat, 01 Aug 2020 18:56:19 +0000 Received: from sanddollar.geekisp.com ([216.168.135.167]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_CHACHA20_POLY1305:256) (Exim 4.92) (envelope-from ) id 1k1wfz-00027h-MR for pgsql-sql@lists.postgresql.org; Sat, 01 Aug 2020 18:56:13 +0000 Received: (qmail 12632 invoked by uid 1003); 1 Aug 2020 18:56:13 -0000 Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=misuse.org; b=DJxT5O4pRrcgr/PVYWsoB1bh7E8W1ca1jN5+gm5m//uOKnbtS78H1oqIwJ+EXmSSYz2JIN7VlIlRPWEW3fi7oiGmdKRJvwDCS5tWx/ctLCH5liieeTVUm/xNp6mCzF+3 ; Received: from unknown (HELO mail-lj1-f181.google.com) (steve@misuse.org@209.85.208.181) by mail.geekisp.com with (ECDHE-RSA-AES128-GCM-SHA256 encrypted) SMTP; 1 Aug 2020 18:56:13 -0000 Received: by mail-lj1-f181.google.com with SMTP id w14so5821476ljj.4 for ; Sat, 01 Aug 2020 11:56:07 -0700 (PDT) X-Gm-Message-State: AOAM532PPnRUyz34MO4S3rXIzCMxX0pPI6aKm7uI3Bdxsjq5I/K5pekj +7Sg1MlDjSSK11ePkkejA2+Zcd5OtchSyvL68As= X-Google-Smtp-Source: ABdhPJzIJCPt/Mhj1wyV+xcL815YK4xYS/dBZBh1WPYoKZ2eE39hD5INEwHwd+rPhZpYdG0PkS4EzAs1/90glGDJY1U= X-Received: by 2002:a2e:8348:: with SMTP id l8mr2677183ljh.111.1596308159134; Sat, 01 Aug 2020 11:55:59 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Steve Midgley Date: Sat, 1 Aug 2020 11:55:47 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: Group by a range of values To: Mike Martin Cc: pgsql-sql Content-Type: multipart/alternative; boundary="000000000000c7032805abd57428" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --000000000000c7032805abd57428 Content-Type: text/plain; charset="UTF-8" On Sat, Aug 1, 2020 at 5:15 AM Mike Martin wrote: > Say I have a field of ints, as for example created by with ordinality or > generate_series, is it possible to group by a range? eq > > 1,2,3,4,5,6,7,8,9,10 > step 3 > so output is > > 1 1 > 2 1 > 3 1 > 4 2 > 5 2 > 6 2 > 7 3 > 8 3 > 9 3 > 10 4 > > thanks > *My solution would be:* select num, ceiling(CAST (num as float)/3) as grp from Agg *Yields:* num grp 1 1 2 1 3 1 4 2 5 2 6 2 7 3 8 3 9 3 10 4 *For DDL of: * CREATE TABLE Agg ("num" int); INSERT INTO Agg ("num") VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10); I created this in SQL Fiddle: http://sqlfiddle.com/#!17/37519e/30/0 --000000000000c7032805abd57428 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Sat, Aug 1, 2020 at 5:15 AM Mike Marti= n <redtux1@gmail.com> wrote:=
Say I have a field of ints, as for exampl= e created by with ordinality or generate_series, is it possible to group by= a range? eq

1,2,3,4,5,6,7,8,9,10
step 3=
so output is

1 1
2 1
3 1
4 2
5 2
6 2
7 3
8 3
9 3
10 4

= thanks

My solution would= be:
select num, ceiling(CAST (num as float)/3) as grp from Agg

Yields:
num grp
1 1
2 1
3 1<= br>4 2
5 2
6 2
7 3
8 3
9 3
10 4

<= div>For DDL of:=C2=A0
CREATE TABLE Agg
=C2=A0 =C2=A0 = ("num" int);
=C2=A0 =C2=A0
INSERT INTO Agg
=C2=A0 =C2= =A0 ("num")
VALUES
=C2=A0 =C2=A0 (1),
=C2=A0 =C2=A0 (2),=
=C2=A0 =C2=A0 (3),
=C2=A0 =C2=A0 (4),
=C2=A0 =C2=A0 (5),
=C2= =A0 =C2=A0 (6),
=C2=A0 =C2=A0 (7),
=C2=A0 =C2=A0 (8),
=C2=A0 =C2= =A0 (9),
=C2=A0 =C2=A0 (10);=C2=A0

I created this in = SQL Fiddle:=C2=A0http://s= qlfiddle.com/#!17/37519e/30/0
--000000000000c7032805abd57428--