This gives the desired result:
DELETE T1 FROM
WHERE EXISTS(
SELECT NULL FROM
WHERE t1.id <> t2.id
AND t1.name = t2.name
AND t1.startdate >= t1.startdate
AND t1.enddate <= t1.enddate
)
http://msdn.microsoft.com/en-us/library/ms188336.aspx
Change . I just noticed that there is one problem. If there are duplicates (the same beginning and end), both will be deleted (none of them match John, even with the same date). Therefore, you need to consider this:
DELETE T1 FROM
WHERE EXISTS(
SELECT NULL FROM
WHERE t1.id <> t2.id
AND t1.name = t2.name
AND t1.startdate > t2.startdate
AND t1.enddate < t2.enddate
OR t1.id <> t2.id
AND t1.name = t2.name
AND t1.startdate = t2.startdate
AND t1.enddate < t2.enddate
OR t1.id <> t2.id
AND t1.name = t2.name
AND t1.startdate > t2.startdate
AND t1.enddate = t2.enddate
OR t1.id > t2.id
AND t1.name = t2.name
AND t1.startdate = t2.startdate
AND t1.enddate = t2.enddate
)
source
share