关于到今天考勤系统连续上班天数的问题或者连续登陆天数的问题(sql)

DateValue(打卡或者登陆日期),ITime(上班时间),OTime(下班时间),StaffID工号,EMPID为员工编号

一个很好的解题思路

找到最近没打卡或者上班记录的最近日期max(DateValue)

select datediff(day,max(DateValue),getdate() ) as DateValue,StaffID from 考勤表 where DateValue
and 上班时间 is null and 下班时间is null group by StaffID

再加筛选条件(最近连续上班超过6天的)

having datediff(day,max(DateValue),getdate())>6
  order by DateValue desc

而网上现在大部分的求连续上班天数的答案都很复杂.要么用游标去循环的.要么建临时表的.

就是从来没有很简单的一条SQL解决的.查询连续登录的天数也是同理.


这是查询25号前未打卡的日期

select max(DateValue) as DateValue,StaffID from 考勤表 where DateValue<'2013-10-25'
and 上班时间is null and 下班时间is null
 group by StaffID
  order by DateValue,StaffID desc

以下为我的查询结果

关于到今天考勤系统连续上班天数的问题或者连续登陆天数的问题(sql)_第1张图片


你可能感兴趣的:(SQL)