sqlserver查询连续签到天数

create table #t
(keyId int identity,actionDate datetime)
insert into #t(actionDate) select distinct CreateDate from CRM_ScoreTransaction WHERE MemberID='1E7DFF7F-51ED-4E21-8471-E892E0326BBD' order BY CreateDate desc
---采用遍历的方式生成用户连续签到的天数 start
declare @i int ,@imax int ,@startDate datetime
set @i=1
select @imax = max(keyId),@startDate =max(actionDate) from #t

while @i <@imax
begin
set @startDate = dateadd(day,-1,@startDate)
set @i =@i+1

if not exists(
select null from #t where keyId =@i and actionDate =@startDate
)
begin
set @i=@i-1
break;
end
end

if @imax is null
begin
set @i=0
end

select convert(varchar(20),@i) signinday

---采用遍历的方式生成用户连续签到的天数 end
go
truncate table #t
drop table #t

posted @ 2019-01-04 11:41  何以平天下  阅读(1454)  评论(0编辑  收藏  举报