Re: Group by a range of values

Поиск
Список
Период
Сортировка
Искать
От
Steve Midgley
Тема
Re: Group by a range of values
Дата
Msg-id
CAJexoSJc_HaF3qM_SsvdpEXWnpSBfja9gBi0Kg8QixehvcZPvw@mail.gmail.com
Ответ на
Список
Дерево обсуждения
Group by a range of values Mike Martin <redtux1@gmail.com>
Re: Group by a range of values Torsten Grust <torsten.grust@gmail.com>
Re: Group by a range of values Rob Sargent <robjsargent@gmail.com>
Re: Group by a range of values Steve Midgley <science@misuse.org>
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 по дате отправления
От: Rob Sargent
Дата:
Сообщение: Re: Group by a range of values
От: Mike Martin
Дата:
FAQ