This is my data:
hmy dtStart dtEnd
14565 2016-07-01 2021-06-30
17237 2016-10-01 2021-06-30
552400 2021-07-01 2021-09-30
822316 2021-10-01 2024-12-30
Here, 14565 and 17237 has a conflict as it contains the same dates in their range.
So I need to remove 14565 from my dataset, because 17237 is newer as its start date is after 14565's start date. We can also understand it as its hmy is larger.
Desired output:
hmy dtStart dtEnd
17237 2016-10-01 2021-06-30
552400 2021-07-01 2021-09-30
822316 2021-10-01 2024-12-30
How can I do it?
I use SQL Server 2018
I tried:
;WITH cte AS (
SELECT hmy, dtStart, dtEnd,
ROW_NUMBER() OVER (PARTITION BY dtStart ORDER BY dtEnd DESC) AS rn
FROM #mytable
)
DELETE FROM #mytable
WHERE hmy IN (SELECT hmy FROM cte WHERE rn > 1)
didn't work as expected.
2条答案
按热度按时间tvokkenx1#
We're going to check if the next dtStart > dtEnd and if not we will not show it.
Fiddle
v8wbuo2f2#
First we sort data by dtStart using
ROW_NUMBER()
then we self join our sorted data to check if there are any overlap of date ranges using the conditionnew_start < existing_end AND new_end > existing_start;
:To remove them then use this query :
Demo here