agora inbox for pgsql-sql@postgresql.org  
help / color / mirror / Atom feed
Group by a range of values
5+ messages / 5 participants
[nested] [flat]

* Group by a range of values
@ 2020-08-01 12:14  Mike Martin <redtux1@gmail.com>
  0 siblings, 2 replies; 5+ messages in thread

From: Mike Martin @ 2020-08-01 12:14 UTC (permalink / raw)
  To: pgsql-sql <pgsql-sql@lists.postgresql.org>

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

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Group by a range of values
@ 2020-08-01 12:34  Torsten Grust <torsten.grust@gmail.com>
  parent: Mike Martin <redtux1@gmail.com>
  1 sibling, 2 replies; 5+ messages in thread

From: Torsten Grust @ 2020-08-01 12:34 UTC (permalink / raw)
  To: pgsql-sql <pgsql-sql@lists.postgresql.org>

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> 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

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Group by a range of values
@ 2020-08-01 12:53  Rob Sargent <robjsargent@gmail.com>
  parent: Torsten Grust <torsten.grust@gmail.com>
  1 sibling, 0 replies; 5+ messages in thread

From: Rob Sargent @ 2020-08-01 12:53 UTC (permalink / raw)
  To: pgsql-sql@lists.postgresql.org



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)





^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Group by a range of values
@ 2020-08-01 18:55  Steve Midgley <science@misuse.org>
  parent: Mike Martin <redtux1@gmail.com>
  1 sibling, 0 replies; 5+ messages in thread

From: Steve Midgley @ 2020-08-01 18:55 UTC (permalink / raw)
  To: Mike Martin <redtux1@gmail.com>; +Cc: pgsql-sql <pgsql-sql@lists.postgresql.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

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Group by a range of values
@ 2020-08-08 11:29  Samed YILDIRIM <samed@reddoc.net>
  parent: Torsten Grust <torsten.grust@gmail.com>
  1 sibling, 0 replies; 5+ messages in thread

From: Samed YILDIRIM @ 2020-08-08 11:29 UTC (permalink / raw)
  To: Torsten Grust <torsten.grust@gmail.com>; pgsql-sql <pgsql-sql@lists.postgresql.org>

<div><div>Hi,</div><div> </div><div>Another (and less optimised :)) alternative;</div><div> </div><div><div>pgsql-sql=# select m,dense_rank() over (order by (m-1)/3) from generate_series(1,10) m;</div><div>m | dense_rank</div><div>----+------------</div><div>1 | 1</div><div>2 | 1</div><div>3 | 1</div><div>4 | 2</div><div>5 | 2</div><div>6 | 2</div><div>7 | 3</div><div>8 | 3</div><div>9 | 3</div><div>10 | 4</div><div>(10 rows)</div><div> </div></div></div><div>Best regards.</div><div>Samed YILDIRIM</div><div> </div><div> </div><div> </div><div>01.08.2020, 15:35, "Torsten Grust" &lt;torsten.grust@gmail.com&gt;:</div><blockquote><div><div style="font-family:'arial' , sans-serif"><div style="font-family:'arial' , sans-serif">Hi,</div><div style="font-family:'arial' , sans-serif"> </div><div style="font-family:'arial' , sans-serif">maybe this does the job already (/ is integer division):</div><div style="font-family:'arial' , sans-serif"> </div><div style="font-family:'arial' , sans-serif">SELECT i, 1 + (i-1) / 3 </div><div style="font-family:'arial' , sans-serif">FROM   generate_series(1,10) AS i;</div><div style="font-family:'arial' , sans-serif"> </div><div style="font-family:'arial' , sans-serif">An expression like (i-1) / 3 could, of course, also be used as partitioning criterion in GROUP BY and/or window functions.</div><div style="font-family:'arial' , sans-serif"> </div><div style="font-family:'arial' , sans-serif">Cheers,</div><div style="font-family:'arial' , sans-serif">  —T</div></div></div> <div><div>On Sat, Aug 1, 2020 at 2:15 PM Mike Martin &lt;<a href="mailto:redtux1@gmail.com">redtux1@gmail.com</a>&gt; wrote:</div><blockquote style="border-left-color:rgb( 204 , 204 , 204 );border-left-style:solid;border-left-width:1px;margin:0px 0px 0px 0.8ex;padding-left:1ex"><div><div>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</div><div> </div><div>1,2,3,4,5,6,7,8,9,10</div><div>step 3</div><div>so output is</div><div> </div><div>1 1</div><div>2 1</div><div>3 1</div><div>4 2</div><div>5 2</div><div>6 2</div><div>7 3</div><div>8 3</div><div>9 3</div><div>10 4</div><div> </div><div>thanks</div></div></blockquote></div> <div> </div>--<div><div><div><div><div><font face="monospace, monospace">| Torsten Grust<br />| <a href="mailto:Torsten.Grust@gmail.com">Torsten.Grust@gmail.com</a></font><br /> </div></div></div></div></div></blockquote>

^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2020-08-08 11:29 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-08-01 12:14 Group by a range of values Mike Martin <redtux1@gmail.com>
2020-08-01 12:34 ` Torsten Grust <torsten.grust@gmail.com>
2020-08-01 12:53   ` Rob Sargent <robjsargent@gmail.com>
2020-08-08 11:29   ` Samed YILDIRIM <samed@reddoc.net>
2020-08-01 18:55 ` Steve Midgley <science@misuse.org>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox