--我也帖一个
create table tb (number varchar(6),日期 smalldatetime)
insert tb
select '001', '2006-01-01' union all
select '001', '2006-01-02' union all
select '001', '2006-01-03' union all
select '001', '2006-01-04' union all
select '001', '2006-01-05' union all
select '001', '2006-01-07' union all
select '001', '2006-01-08' union all
select '003', '2006-01-09' union all
select '003', '2006-01-10' union all
select '003', '2006-01-11'
select number,min(日期) as 开始时间,max(日期) as 结束时间
from(
select a.number,a.日期,count(1) as ii
from tb a
join tb b on b.number=a.number and b.日期<=a.日期
group by a.number,a.日期
)c
group by number, DATEADD(day,-c.ii,日期)
go
drop table tb
/*
number 开始时间 结束时间
------ ------------------------------- ----------------------------------
001 2006-01-01 00:00:00 2006-01-05 00:00:00
001 2006-01-07 00:00:00 2006-01-08 00:00:00
003 2006-01-09 00:00:00 2006-01-11 00:00:00
(3 row(s) affected)
*/