• Expanding a bit on Eric's reply:

    1. For the primary key, I would just use MemNo, YMDStart. Then I would also add a UNIQUE constraint on MemNo, YMDEnd.

    2. Slightly simpler (and probably more efficient) version of his code:

    SELECT A.MemNo, A.YMDStart, A.YMDEnd, B.YMDStart, B.YMDEnd

    FROM zzz_Overlapping A

    INNER JOIN zzz_Overlapping B

    ON B.MemNo = A.MemNo

    AND B.YMDStart < A.YMDEnd

    AND A.YMDStart < B.YMDEnd

    AND A.YMDStart < B.YMDStart;

    (Note that this code, as well as Eric's code, does rely on EndDate in any row being larger than StartDate. Rows with EndDate before StartDate do not really define a proper interval anyway, and you should not expect to get relevant results from them. And yes, you do have such a "weird interval" row in your sample data).


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/