I might be missing something, but can you not use MIN
instead of TOP 1 ... ORDER BY
, since you are ordering by EndDate, StartDate DESC, and only selecting EndDate, the start date is not relevant to the sort, it is only considered when you have 2 end dates that are the same, but since you are only selecting end date it doesn't matter which of the two (or more end dates) are used:
SELECT D.StartDate,
( SELECT MIN(E.EndDate)
FROM Dates E
WHERE E.EndDate >= D.EndDate
AND NOT EXISTS
( SELECT 1
FROM Dates E2
WHERE E.StartDate < E2.StartDate
AND E.EndDate > E2.StartDate
)
) EndDate
FROM Dates D
WHERE NOT EXISTS
( SELECT 1
FROM Dates D2
WHERE D.StartDate < D2.EndDate
AND D.EndDate > D2.EndDate
);