Quantcast
Channel: Answers for "Selecting multiple ranges from a set of dates"
Viewing all articles
Browse latest Browse all 12

Answer by Blackhawk-17

$
0
0
Borrowing heavily from Phil Factor's answer I have a partial solution but one that raises my own question. I can get (sort of) the start and end times of the error bunches and I also get the single entry. I do this without the quirky update (which I thought was a cool and elegant solution). I can "see" where the breaks are because of the sequential numbers but I just can't get the output the OP requested. How do I take this to the next level? declare @Dt table (TestDate DateTime) insert into @dt (testdate) values (convert(datetime,'26/07/2010 1:00:03',103)), (convert(datetime,'26/07/2010 1:00:04',103)), (convert(datetime,'26/07/2010 1:00:05',103)), (convert(datetime,'26/07/2010 1:00:06',103)), (convert(datetime,'26/07/2010 1:00:07',103)), (convert(datetime,'26/07/2010 1:00:25',103)), (convert(datetime,'26/07/2010 1:00:50',103)), (convert(datetime,'26/07/2010 1:00:51',103)), (convert(datetime,'26/07/2010 1:00:52',103)), (convert(datetime,'26/07/2010 1:00:53',103)), (convert(datetime,'26/07/2010 1:00:54',103)), (convert(datetime,'26/07/2010 1:00:55',103)), (convert(datetime,'26/07/2010 1:00:56',103)), (convert(datetime,'26/07/2010 1:00:57',103)), (convert(datetime,'26/07/2010 1:00:58',103)), (convert(datetime,'26/07/2010 1:00:59',103)), (convert(datetime,'26/07/2010 1:01:00',103)), (convert(datetime,'26/07/2010 1:01:01',103)), (convert(datetime,'26/07/2010 1:01:02',103)), (convert(datetime,'26/07/2010 1:01:03',103)), (convert(datetime,'26/07/2010 1:01:04',103)), (convert(datetime,'26/07/2010 1:50:37',103)), (convert(datetime,'26/07/2010 1:50:38',103)), (convert(datetime,'26/07/2010 1:50:39',103)), (convert(datetime,'26/07/2010 1:50:40',103)), (convert(datetime,'26/07/2010 1:50:41',103)), (convert(datetime,'26/07/2010 1:50:42',103)), (convert(datetime,'26/07/2010 1:50:43',103)), (convert(datetime,'26/07/2010 1:50:44',103)), (convert(datetime,'27/07/2010 2:20:37',103)), (convert(datetime,'27/07/2010 2:20:38',103)), (convert(datetime,'27/07/2010 2:20:39',103)), (convert(datetime,'27/07/2010 2:20:40',103)), (convert(datetime,'27/07/2010 2:20:41',103)), (convert(datetime,'27/07/2010 2:20:42',103)), (convert(datetime,'27/07/2010 2:20:43',103)), (convert(datetime,'27/07/2010 2:20:44',103)) SELECT MIN(TestDate) AS [Date], 1 AS [Sequence] FROM @Dt UNION ALL SELECT dt1.TestDate, dt1.theOrder FROM ( SELECT TestDate, ROW_NUMBER() OVER(ORDER BY TestDate) AS theOrder FROM @Dt ) AS dt1 INNER JOIN ( SELECT TestDate, ROW_NUMBER() OVER(ORDER BY TestDate) AS theOrder FROM @Dt ) AS dt2 ON dt1.theOrder + 1 = dt2.theOrder INNER JOIN ( SELECT TestDate, ROW_NUMBER() OVER(ORDER BY TestDate) AS theOrder FROM @Dt ) AS dt3 ON dt1.theOrder - 1 = dt3.theOrder WHERE DATEADD(second, -1, dt1.TestDate) <> dt3.TestDate OR DATEADD(second, 1, dt1.TestDate) <> dt2.TestDate UNION ALL SELECT MAX(TestDate), COUNT(*) FROM @Dt The following result set is produced: Date Sequence 2010-07-26 01:00:03.000 1 2010-07-26 01:00:07.000 5 2010-07-26 01:00:25.000 6 2010-07-26 01:00:50.000 7 2010-07-26 01:01:04.000 21 2010-07-26 01:50:37.000 22 2010-07-26 01:50:44.000 29 2010-07-27 02:20:37.000 30 2010-07-27 02:20:44.000 37 While it is intuitively obvious where the start and end of the bunches are I can't take it logically forward for the final solution to the OP's question,

Viewing all articles
Browse latest Browse all 12

Trending Articles