declare @T table ( [Date] datetime, Code char(1) ) inse...


Please login or register to vote for this query.

(click on this box to dismiss)

Stack Overflow

Q&A for programmers

-- Enter Query Title
-- Enter Query Description

declare @T table
  [Date] datetime,
  Code char(1)

insert into @T values

;with C as
  select *,
         datediff(day, 0, [Date]) - row_number() over(partition by Code 
                                                      order by [Date]) as rn
  from @T
select min([Date]) as StartDate,
       max([Date]) as EndDate,
from C
group by Code, rn

Enter Parameters

Switch sites:
loading Hold tight while we fetch your results