需要找到今天没有签到但已结账的员工

时间:2016-07-04 07:08:24

标签: sql

enter image description here

我已经添加了忘记在1日签到但在同一天签出的员工的屏幕截图。我试过这个查询

select t.*
      from M_AttendanceResult t outer apply
           (select count(t2.checkintime) as cnt
            from M_AttendanceResult t2
            where t2.AccountID = t.AccountID and
                  coalesce(t2.checkintime, t2.checkouttime) <= coalesce(t.checkintime, t.checkouttime)
                 -- and CheckOutTime between convert(varchar(10),getdate()-3,120) and convert(varchar(10),getdate()+1,120)
           ) ins outer apply
           (select count(t2.checkouttime) as cnt
            from M_AttendanceResult t2
            where t2.AccountID = t.AccountID and
                  coalesce(t2.checkouttime, t2.checkintime) <= coalesce(t.checkouttime, t.checkintime)
                  and CheckOutTime between convert(varchar(10),getdate()-3,120) and convert(varchar(10),getdate()+1,120)
           ) outs
where t.checkouttime is not null and outs.cnt > ins.cnt 

但它给我空白输出。我正在使用mssql2008

我也试过这个

select * from M_AttendanceResult
where 
 CheckOutTime between convert(varchar(10),getdate()-3,120) and convert(varchar(10),getdate(),120)
and not exists
(select CheckInTime from M_AttendanceResult
where CheckInTime is not null
and CheckInTime between convert(varchar(10),getdate()-3,120) and convert(varchar(10),getdate(),120))

但仍然是空白输出。

3 个答案:

答案 0 :(得分:1)

如果没有其他行与checkin相同,请使用NOT EXISTS返回一行。

select * from tablename t1
where t1.checkouttime is not null
  and not exists (select * from tablename t2
                  where date(t2.checkindate) = date(t1.checkoutdate)

答案 1 :(得分:0)

尝试此查询:

SELECT t.* from M_AttendanceResult t 
          WHERE t.AccountID IN 
          (SELECT AccountID from M_AttendanceResult 
               WHERE CAST(CheckInTime AS DATE) = CAST(GETDATE() AS DATE))

答案 2 :(得分:0)

选择ma.AccountName,m.AccountID,ma.AccountIndex,CheckInTime,CheckOutTime,ShiftID,
来自M_AttendanceResult的DepartmentAccountID为m
      加入M_Account作为马       on m.AccountID = ma.AccountID
            where(转换(varchar(10),getdate() - 1,120之间的CheckInTime和转换(varchar(10),getdate(),120)
      要么      转换(varchar(10),getdate() - 1,120)和转换(varchar(10),getdate(),120)之间的CheckOutTime)      并且CheckInTime为空     并不存在         (从M_AttendanceResult中选择*为mm
          其中CheckInTime之间的转换(varchar(10),getdate() - 1,120)和转换(varchar(10),getdate(),120)
          和mm.AccountID = m.AccountID)

相关问题