【发布时间】:2021-02-13 03:20:19
【问题描述】:
我有一个包含 3 列的表格。我想为每个 id 从其 start_date 到 end_date 创建一个日期列,如果没有 end_date 然后到今天。
如何在 Postgres 中激活它?谢谢!
示例表:
+------+------------+------------+--+
| id | start_date | end_date | |
+------+------------+------------+--+
| 47ef | 2020-09-25 | 2020-09-30 | |
| b67c | 2020-09-21 | 2020-10-02 | |
| 9f9e | 2020-08-28 | 2020-10-02 | |
| 854a | 2020-07-29 | 2020-10-05 | |
| a316 | 2020-05-01 | NULL | |
+------+------------+------------+--+
id '47ef' 的期望输出:
+------+------------+------------+------------+
| id | start_date | end_date | date |
+------+------------+------------+------------+
| 47ef | 2020-09-25 | 2020-09-30 | 2020-09-25 |
| 47ef | 2020-09-25 | 2020-09-30 | 2020-09-26 |
| 47ef | 2020-09-25 | 2020-09-30 | 2020-09-27 |
| 47ef | 2020-09-25 | 2020-09-30 | 2020-09-28 |
| 47ef | 2020-09-25 | 2020-09-30 | 2020-09-29 |
| 47ef | 2020-09-25 | 2020-09-30 | 2020-09-30 |
+------+------------+------------+------------+
【问题讨论】:
-
为什么是
date列?您已经有了日期范围;start_date到end_date
标签: postgresql generate-series