SQL Server 员工考勤数据检查连续上班 7 天的 SQL 语句

xinggang · 2016年09月29日 · 159 次阅读

验厂审核要求员工不能连续上班超 6 天,这里的 SQL 语句可以从考勤记录数据表中检查出哪些人连续上班 7 天。** 首先说明一下考勤记录数据表主要字段:
20160929_0002
然后是检查语句:

--查询连续上班7天的员工
with
t1 as (select distinct EmployeeGuid,b.Name,AttDate,dateadd(day,6,AttDate) Date2 
          from PM_Att_Record a inner join PM_Att_Employee b on a.CheckYear=b.CheckYear and a.CheckMonth=b.CheckMonth and a.EmployeeGuid=b.Guid
          where (a.CheckYear=@CheckYear2 and a.CheckMonth=@CheckMonth2 and AttDate>=@BeginDate2 or a.CheckYear=@CheckYear and a.CheckMonth=@CheckMonth and b.OrgFrameGuid4 like @OrgFrame)
                and a.WorkHours>0
       ),
t2 as (select a.EmployeeGuid,a.Name,b.AttDate,b.Date2,count(*) as days 
        from t1 a,t1 b 
        where a.EmployeeGuid=b.EmployeeGuid and a.AttDate between b.AttDate and b.Date2 group by a.EmployeeGuid,a.Name,b.AttDate,b.Date2)
select t2.Name,min(t2.Date2),'连续7天上班'
  from t2 where days=7
  group by t2.Name
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册