首页 > 数据库技术 > 详细

sqlserver查询连续签到天数

时间:2019-01-04 14:17:26      阅读:270      评论:0      收藏:0      [点我收藏+]

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

sqlserver查询连续签到天数

原文:https://www.cnblogs.com/heyiping/p/10219004.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!