根据列值重复行数
Repeat row number of times based on column value
我有 table 看起来像这样:
| name | start_date | duration_day|
========================================
| A | 2015-01-01 | 3 |
| B | 2015-01-02 | 2 |
现在我想得到这样的输出:
| name | date |
=====================
| A | 2015-01-01 |
| A | 2015-01-02 |
| A | 2015-01-03 |
| B | 2015-01-02 |
| B | 2015-01-03 |
我如何在 PostgreSQL 中执行此操作?
select
name,
start_date + generate_series(0, duration_day - 1)
from
your_table;
借用 , you can generate a series from the duration_day
value with the generate_series()
table function in the row source list. The function uses the duration_day
value from my_table
through an implicit lateral join.
SELECT name, start_date + n AS date
FROM my_table, generate_series(0, duration_day - 1) AS x(n);
我有 table 看起来像这样:
| name | start_date | duration_day|
========================================
| A | 2015-01-01 | 3 |
| B | 2015-01-02 | 2 |
现在我想得到这样的输出:
| name | date |
=====================
| A | 2015-01-01 |
| A | 2015-01-02 |
| A | 2015-01-03 |
| B | 2015-01-02 |
| B | 2015-01-03 |
我如何在 PostgreSQL 中执行此操作?
select
name,
start_date + generate_series(0, duration_day - 1)
from
your_table;
借用 duration_day
value with the generate_series()
table function in the row source list. The function uses the duration_day
value from my_table
through an implicit lateral join.
SELECT name, start_date + n AS date
FROM my_table, generate_series(0, duration_day - 1) AS x(n);