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