sql查询结果中的重复(重复)行

icl*_*126 2 sql postgresql postgresql-9.4

假设我有一个有两行的表

 id | value |
----+-------+
 1  |   2   |
 2  |   3   |
Run Code Online (Sandbox Code Playgroud)

我想编写一个查询,它将根据值复制(重复)每一行.
我想要这个结果(总共5行):

 id | value |
----+-------+
 1  |   2   |
 1  |   2   |
 2  |   3   |
 2  |   3   |
 2  |   3   |
Run Code Online (Sandbox Code Playgroud)

我正在使用PostgreSQL 9.4.

Gor*_*off 9

你可以使用generate_series():

select t.id, t.value
from (select t.id, t.value, generate_series(1, t.value)
      from t 
     ) t;
Run Code Online (Sandbox Code Playgroud)

您可以使用横向连接执行相同的操作:

select t.id, t.value
from t, lateral
     generate_series(1, t.value);
Run Code Online (Sandbox Code Playgroud)