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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM