问题
Let's say I have a table with two rows
id | value |
----+-------+
1 | 2 |
2 | 3 |
I want to write a query that will duplicate (repeat) each row based on the value.
I want this result (5 rows total):
id | value |
----+-------+
1 | 2 |
1 | 2 |
2 | 3 |
2 | 3 |
2 | 3 |
I'm using PostgreSQL 9.4.
回答1:
You can use generate_series()
:
select t.id, t.value
from (select t.id, t.value, generate_series(1, t.value)
from t
) t;
You can do the same thing with a lateral join:
select t.id, t.value
from t, lateral
generate_series(1, t.value);
来源:https://stackoverflow.com/questions/35293084/duplicate-repeat-rows-in-sql-query-result