如何每2行创建范围

时间:2018-10-06 09:37:02

标签: sql postgresql

我有下表:

+------------+
|    date    |
+------------+
| 2018-07-01 |
| 2018-09-01 |
| 2018-08-01 |
+------------+

如何查询以获得以下结果?

+------------+------------+
|  min_date  |  max_date  |
+------------+------------+
| 2001-01-01 | 2018-07-01 |
| 2018-07-01 | 2018-08-01 |
| 2018-08-01 | 2018-09-01 |
+------------+------------+

2 个答案:

答案 0 :(得分:3)

lag()接受三个参数,第三个参数是默认参数,因此您可以这样写:

select lag(date, 1, '2001-01-01' as date) over (order by date) as min_date,
       date as max_date
from t;

答案 1 :(得分:2)

类似的东西:

/page/2