Determine contiguous dates in SQL gaps and islands
问题 I have a situation where a single patient can receive multiple services. These services can have overlapping dates, and can gaps and islands. I am trying to write a query that will show the contiguous length of time that the patient was receiving some kind of service. Table is as follows: CREATE TABLE #tt (Patient VARCHAR(10), StartDate DATETIME, EndDate DATETIME) INSERT INTO #tt VALUES ('Smith', '2014-04-13', '2014-06-04'), ('Smith', '2014-05-07', '2014-05-08'), ('Smith', '2014-06-21', '2014