When there is a gap between commitments of the same type, the fact should contain multiple records that show the accurate start and end date of each continuous commitment. An example of this is patid 1001 and when there is a continuation of the same status without a gap, it should be a single record.
CREATE TABLE #legal_data (
ClaimID VARCHAR(20)
,dim_legalstat_key int--dimensionkey
,[order_start_date] DATE
,[order_end_date] DATE
,[days_committed] int----days between order_start_date & order_end_date
)
INSERT INTO #legal_data
VALUES
('1001','11','2022-05-11','2022-10-29','171')
,('1001','131','2022-07-15','2023-03-19','247')
,('1001','116','2023-03-14','2023-03-20','6')
,('1001','11','2023-03-20','2023-03-23','3')
,('1207','11','2022-09-13','2023-03-12','180')
,('1207','11','2023-03-10','2023-03-23','13')
,('1924','2','2021-12-18','2022-06-19','183')
,('1924','2','2022-06-19','2023-12-20','184')
,('1842','77','2021-02-20','2022-06-17','482')
,('1842','77','2022-06-18','2023-12-20','550')
,('1661','22','2022-02-14','2023-03-20','399')
,('1661','22','2022-02-14','2023-03-23','402')
,('1553','4','2022-01-14','2022-02-12','29')---
,('1553','4','2022-02-14','2023-03-23','402')
------desired result
CREATE TABLE #legal_Result (
ClaimID VARCHAR(20)
,dim_legalstat_key int--dimensionkey
,[order_start_date] DATE
,[order_end_date] DATE
,[days_committed] int----days between order_start_date & order_end_date
)
INSERT INTO #legal_Result
VALUES
('1001','11','2022-05-11','2022-10-29','171')
,('1001','131','2022-07-15','2023-03-19','247')
,('1001','116','2023-03-14','2023-03-20','6')
,('1001','11','2023-03-20','2023-03-23','3')
,('1207','11','2022-09-13','2023-03-23','191')
,('1924','2','2021-12-18','2023-12-20','732')
,('1842','77','2021-02-20','2023-12-20','1033')--not working
,('1661','22','2022-02-14','2023-03-23','402') ---
,('1553','4','2022-01-14','2022-02-12','29')--anything the
,('1553','4','2022-02-14','2023-03-23','402')
select * from #legal_data
select * from #legal_Result