SELECT period.name, period.startdate
FROM period
LEFT OUTER JOIN period AS period_end ON period.name = period_end.name
AND DATEADD(dd, 1, period.enddate) = period_end.startdate
WHERE (period_end.startdate IS NULL)
Но сюда будет попадать последний период.
Будем считать, последний период "закрыт" максимальной для
name
датой, то есть:
ddd 2019-01-01 2019-05-01
ddd 2019-05-02 2019-06-07
ddd 2019-06-10 2019-09-04
ddd 2019-09-05 2999-12-31
aaa 2019-09-01 2019-09-10
aaa 2019-09-12 2019-12-31
aaa 2020-01-01 2020-12-31
SELECT period.name, period.startdate, period.enddate
FROM period
LEFT OUTER JOIN period AS period_end ON period.name = period_end.name
AND DATEADD(dd, 1, period.enddate) = period_end.startdate
WHERE (period_end.startdate IS NULL)
AND (period.enddate <> (SELECT MAX(enddate) FROM period AS period_last
WHERE (name = period.name)))