Re: Group by a range of values - Mailing list pgsql-sql

From Steve Midgley
Subject Re: Group by a range of values
Date
Msg-id CAJexoSJc_HaF3qM_SsvdpEXWnpSBfja9gBi0Kg8QixehvcZPvw@mail.gmail.com
Whole thread Raw
In response to Group by a range of values  (Mike Martin <redtux1@gmail.com>)
List pgsql-sql
On Sat, Aug 1, 2020 at 5:15 AM Mike Martin <redtux1@gmail.com> 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

pgsql-sql by date:

Previous
From: Rob Sargent
Date:
Subject: Re: Group by a range of values
Next
From: Mike Martin
Date:
Subject: Error - Arrays must contain only scalars and other arrays when array element is an array