[postgis-users] Create mutliple rows from a field

Nathan Wagner nw at hydaspes.if.org
Thu May 18 10:20:34 PDT 2017


On Thu, May 18, 2017 at 07:05:39PM +0200, Olivier Lepr?tre wrote:

[ excess newlines snipped]

> I have a table with 2 columns : id, x (x is an int from 0 to n)

> Is it possible to create a query that will return x ranked rows for
> each id ?

> with

> id       x
> A        2
> B        3
> C        1
> D       4

> will return

> A        1
> A        2
> B        1
> B        2
> B        3
> C        1
> D       1
> D       2
> D       3
> D       4

create table foo (id text, x integer);
insert into foo
values ('A',2),('B',3),('C',1),('D',4)
;
select id, generate_series(1, x) as r from foo;

-- 
nw


More information about the postgis-users mailing list