agora inbox for pgsql-sql@postgresql.org
help / color / mirror / Atom feedFrom: Rob Sargent <robjsargent@gmail.com>
To: pgsql-sql@lists.postgresql.org
Subject: Re: Group by a range of values
Date: Sat, 1 Aug 2020 06:53:38 -0600
Message-ID: <cc130272-05d6-47a5-8674-198235196188@gmail.com> (raw)
In-Reply-To: <CAGqkgpgDH7O_AqtPcY91y2TrPaEF4oe-jFwuqXeuFCedgSAUXw@mail.gmail.com>
References: <CAOwYNKYcQR_Fzx0=L+5LBh6zqDXQuA3_a+ggAm5RZ3G6zHNbOQ@mail.gmail.com>
<CAGqkgpgDH7O_AqtPcY91y2TrPaEF4oe-jFwuqXeuFCedgSAUXw@mail.gmail.com>
On 8/1/20 6:34 AM, Torsten Grust wrote:
> Hi,
>
> maybe this does the job already (/ is integer division):
>
> SELECT i, 1 + (i-1) / 3
> FROM generate_series(1,10) AS i;
>
> An expression like (i-1) / 3 could, of course, also be used as
> partitioning criterion in GROUP BY and/or window functions.
>
> Cheers,
> —T
>
> On Sat, Aug 1, 2020 at 2:15 PM Mike Martin <redtux1@gmail.com
> <mailto: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
>
>
>
> --
> | Torsten Grust
> | Torsten.Grust@gmail.com <mailto:Torsten.Grust@gmail.com>
>
My version is as follows, the point being that the "grouping" requested
is simply an ordering of the step mechanism. Naturally this series is
generated in order shown but real data for val likely won't be.
test=# with ts as (select generate_series(1,10) as val) select s.val,
(s.val /3)+1 as ord from ts as s order by ord;
val | ord
-----+-----
1 | 1
2 | 1
3 | 2
4 | 2
5 | 2
6 | 3
7 | 3
8 | 3
9 | 4
10 | 4
(10 rows)
view thread (5+ messages) latest in thread
Message-ID: <cc130272-05d6-47a5-8674-198235196188@gmail.com>
Permalink: ../cc130272-05d6-47a5-8674-198235196188@gmail.com/
Also on: postgresql.org/message-id/cc130272-05d6-47a5-8674-198235196188@gmail.com
reply
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 using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: robjsargent@gmail.com, pgsql-sql@lists.postgresql.org
Subject: Re: Group by a range of values
In-Reply-To: <cc130272-05d6-47a5-8674-198235196188@gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox