Something like thisCTE method;With CTEAS(SELECT ROW_NUMBER() OVER (PARTITION BY id ORDER BY contactdate) AS Rn,*FROM table)SELECT c1.id,c1.contactdate,DATEDIFF(dd,c1.contactdate,c2.contactdate) AS daysbetweenFROM CTE c1LEFT JOIN CTE c2ON c2.id = c1.idAND c2.Rn = c1.Rn + 1
Apply methodSELECT t.id,t.contactdate,DATEDIFF(dd,t.contactdate,t1.contactdate) AS daysbetweenFROM table tOUTER APPLY (SELECT MIN(contactdate) AS nextdate FROM table WHERE id = t.id AND contactdate > t.contactdate )t1
------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs