我有一个表,上面有名称、位置、开始日期和结束日期,如下所示:
+------+----------+-----------+-----------+-----------+
| name | location | startdate | endate | is_active |
+------+----------+-----------+-----------+-----------+
| A | delhi | 3/26/2019 | 3/26/2019 | 1 |
| A | delhi | 3/27/2019 | 3/27/2019 | 1 |
| A | delhi | 3/28/2019 | 3/28/2019 | 1 |
| A | delhi | 3/31/2019 | 3/31/2019 | 1 |
+------+----------+-----------+-----------+-----------+
需要这样更新:
+------+----------+-----------+-----------+-----------+
| name | location | startdate | endate | is_active |
+------+----------+-----------+-----------+-----------+
| A | delhi | 3/26/2019 | 3/28/2019 | 1 |
| A | delhi | 3/27/2019 | 3/27/2019 | 0 |
| A | delhi | 3/28/2019 | 3/28/2019 | 0 |
| A | delhi | 3/31/2019 | 3/31/2019 | 1 |
+------+----------+-----------+-----------+-----------+
如果 startdate
如果是连续的,则用最后一个连续的结束日期更新结束日期 startdate
以及更新 is_active = 0
连续开始日期
1条答案
按热度按时间zqdjd7g91#
这是一个缺口和孤岛问题。下面是一种使用
lag()
以及累积的sum()
定义组。最后一步是逻辑:db小提琴演示: