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 [email protected]+1
if not exists(
select null from #t where keyId [email protected] and actionDate [email protected]
)
begin
set @[email protected]
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
原文地址:https://www.cnblogs.com/heyiping/p/10219004.html