日期:2014-05-18 浏览次数:20620 次
create table LoginTable (name varchar(10),logindate date) insert into LoginTable values ('A','2011-03-07'), ('A','2011-03-1'), ('A','2011-03-2'), ('A','2011-03-3'), ('A','2011-03-6'), ('A','2011-03-8'), ('A','2011-03-10'), ('A','2011-03-11'), ('A','2011-03-12'), ('A','2011-03-15'), ('A','2011-03-16'), ('A','2011-03-20'), ('A','2011-03-25'), ('A','2011-03-29'); with d as ( select logindate,(select min(b.logindate) from LoginTable b where b.logindate>=a.logindate and not exists (select * from LoginTable c where c.logindate=dateadd(dd,1,b.logindate))) as grp from LoginTable a ), m as( select min(logindate) as start_range,max(logindate) as end_range from d group by grp) select max(DATEDIFF(DD,start_range,end_range)+1) as maxday from m where DATEDIFF(DD,start_range,end_range)<>0 /* maxday 3 */
------解决方案--------------------