日期:2014-05-18 浏览次数:20578 次
create table #loginlog(logintime datetime,u_id int) insert into #loginlog select '2011-12-16',907 insert into #loginlog select '2011-12-17',907 insert into #loginlog select '2011-12-18',907 insert into #loginlog select '2011-12-14',1100 insert into #loginlog select '2011-12-15',1100 insert into #loginlog select '2011-12-16',1100 insert into #loginlog select '2011-12-13',1200 insert into #loginlog select '2011-12-14',1200 insert into #loginlog select '2011-12-16',1200 insert into #loginlog select '2011-12-17',1200 insert into #loginlog select '2011-12-18',1200 go declare @date datetime SET @date='2011-12-16' select a.u_id from #loginlog a join (select u_id from #loginlog where logintime=DATEADD(D,-1,@date))b on b.u_id=a.u_id join (select u_id from #loginlog where logintime=DATEADD(D,-2,@date))c on c.u_id=a.u_id where logintime=@date drop table #loginlog
------解决方案--------------------
select distinct u_id,count(*) as 连续登陆次数 from loginlog
where logintime between convert(varchar(10),DATEADD(DD,-3,'2011-12-18'),120)
and CONVERT(varchar(10),'2011-12-18',120)
group by u_id
having COUNT(*)>=3