SQL 查找 ID 的开始和结束日期
SQL to find start and end date for an ID
我有一个带有 ID、start_date 和 end_date 列的 table。
Table:
ID start_date end_date
1 01/01/2017 01/01/2018
1 01/01/2018 01/01/2019
1 01/01/2019 01/01/2020
2 01/01/2016 01/01/2017
2 01/01/2017 01/01/2018
2 01/01/2019 01/01/2020
我想编写查询以获得以下输出:
输出:
ID start_date end_date
1 01/01/2017 01/01/2020
2 01/01/2016 01/01/2018
2 01/01/2019 01/01/2020
这是一种空隙和孤岛的形式。
在这种情况下,我的建议是使用累积最大值来查看是否与前面的行有任何重叠,并使用它来确定 "island" 从哪里开始。然后,使用累积和来定义孤岛和聚合:
select id, min(start_date), max(end_date
from (select t.*,
sum(case when prev_end_date >= start_date then 0 else 1 end) over (partition by id order by start_date) as grp
from (select t.*,
lag(end_date) over (partition by id
order by start_date
rows between unbounded preceding and 1 preceding
) as prev_end_date
from t
) t
) t
group by id, grp;
你可以做一个累加和来解决间隙和孤岛问题的这个变体:
select
id,
min(start_date) start_date,
max(end_date) end_date
from (
select
t.*,
sum(case when start_date = lag_end_date then 0 else 1 end)
over(partition by id order by start_date) grp
from (
select
t.*,
lag(end_date) over(partition by id order by start_date) lag_end_date
from mytable t
) t
) t
group by id, grp
order by id, grp
ID | START_DATE | END_DATE
-: | :--------- | :---------
1 | 01/01/2017 | 01/01/2020
2 | 01/01/2016 | 01/01/2018
2 | 01/01/2019 | 01/01/2020
Select id, Min(startdate), Max(case
when
lag(enddate)
over
(partition by id order by id) =startdate
then
Enddate end) from table group by
id;
我有一个带有 ID、start_date 和 end_date 列的 table。
Table:
ID start_date end_date
1 01/01/2017 01/01/2018
1 01/01/2018 01/01/2019
1 01/01/2019 01/01/2020
2 01/01/2016 01/01/2017
2 01/01/2017 01/01/2018
2 01/01/2019 01/01/2020
我想编写查询以获得以下输出:
输出:
ID start_date end_date
1 01/01/2017 01/01/2020
2 01/01/2016 01/01/2018
2 01/01/2019 01/01/2020
这是一种空隙和孤岛的形式。
在这种情况下,我的建议是使用累积最大值来查看是否与前面的行有任何重叠,并使用它来确定 "island" 从哪里开始。然后,使用累积和来定义孤岛和聚合:
select id, min(start_date), max(end_date
from (select t.*,
sum(case when prev_end_date >= start_date then 0 else 1 end) over (partition by id order by start_date) as grp
from (select t.*,
lag(end_date) over (partition by id
order by start_date
rows between unbounded preceding and 1 preceding
) as prev_end_date
from t
) t
) t
group by id, grp;
你可以做一个累加和来解决间隙和孤岛问题的这个变体:
select
id,
min(start_date) start_date,
max(end_date) end_date
from (
select
t.*,
sum(case when start_date = lag_end_date then 0 else 1 end)
over(partition by id order by start_date) grp
from (
select
t.*,
lag(end_date) over(partition by id order by start_date) lag_end_date
from mytable t
) t
) t
group by id, grp
order by id, grp
ID | START_DATE | END_DATE -: | :--------- | :--------- 1 | 01/01/2017 | 01/01/2020 2 | 01/01/2016 | 01/01/2018 2 | 01/01/2019 | 01/01/2020
Select id, Min(startdate), Max(case
when
lag(enddate)
over
(partition by id order by id) =startdate
then
Enddate end) from table group by
id;