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
I looked at the solution proposed by @dougp. It works well with "normally" ordered data. I think, problem is with "chaotically" ordered data such as this:
For this question, for example
Query result for this rows
Expected result
Maybe the data is ordered "correctly" and there will be no such case.
This task is interesting. I see, my view of the solution would be similar to @dougp's solution. Seeing a possible error on the test data, I will propose a recursive solution for consideration.
Example
Update1.
For concatenated ranges (start_date=next day from end_date =end_date+1day) I'll add some checkings. (for example case with ClaimId 1842)
First CTE
ndataunite rows with the same start_date to 1 row.Base part of recursion query
rselects first row from group of intersected or concatenated rows.Recursive part of query
consecutively combinesall other rows from this group.Corrected query
Example here