Я попробовал ниже на SQL сервере, и он работает:
Настройка теста
CREATE TABLE Employee(EmployeeId INT, fromDate date, todate date, Placename VARCHAR(100), PlaceCode VARCHAR(100))
INSERT INTO Employee
VALUES(1111,'2019-07-25','2099-02-14','CHENNAI','MAA'),
(1111,'2020-02-15','2020-02-23','DELHI','DEL');
Запрос на выполнение
;WITH CTE_Ranges AS
(
SELECT EmployeeId, fromdate, lag(fromdate,1) OVER(PARTITION BY EmployeeId ORDER BY fromdate) previousfromDate,todate
, lead(todate,1) OVER(PARTITION BY EmployeeId ORDER BY todate) nexttodate, placename, placecode from Employee
)
--Handle the Maximum and minimum dates
SELECT * FROM
(
SELECT EmployeeId, fromdate, DATEADD(day,-1,lead(fromdate) over(partition by EmployeeId ORDER BY fromDate)) as todate, PlaceName, PlaceCode
FROM CTE_Ranges
UNION ALL
SELECT EmployeeId, DATEADD(day,1,lag(todate) over(partition by EmployeeId ORDER BY todate)) as fromdate, todate,PlaceName, PlaceCode
FROM CTE_Ranges) AS t
WHERE fromdate is not null and todate is not null
UNION ALL
--Now handle normal date ranges
SELECT EmployeeId, fromDate,todate, placename, placecode
from cte_ranges
WHERE previousfromdate is not null and nexttodate is not null
order by fromdate
Resultset
+------------+------------+------------+-----------+-----------+
| EmployeeId | fromdate | todate | PlaceName | PlaceCode |
+------------+------------+------------+-----------+-----------+
| 1111 | 2019-07-25 | 2020-02-14 | CHENNAI | MAA |
| 1111 | 2020-02-15 | 2020-02-23 | DELHI | DEL |
| 1111 | 2020-02-24 | 2099-02-14 | CHENNAI | MAA |
+------------+------------+------------+-----------+-----------+