1

我有一张表,它根据用户名存储员工通过 Time Machine 的签到时间。如果员工多次打卡,那么他的签到记录就会有多个。所有在第一条记录后 1 分钟内出现的记录均无效,必须删除。如果比第一条记录晚一分钟的记录有效,不得删除。那么这个第二个有效记录也可能包含在这个第二个有效记录的1分钟内无效的后续记录。这些记录也应该被删除,依此类推。如何在当前日期的员工的所有签到记录上执行此操作?flag 字段不在 db 中,它只是说明哪个记录有效,哪个记录无效。

The Data in the db is as follows.

Username               Checktime                       CheckType          Flag
 HRA001            7/29/2012 8:16:44 AM                Check-In           Valid 
 HRA001            7/29/2012 8:16:46 AM                Check-In           Invalid
 HRA001            7/29/2012 8:16:50 AM                Check-In           Invalid
 HRA001            7/29/2012 8:17:30 AM                Check-In           Invalid 
 HRA001            7/29/2012 8:17:50 AM                Check-In           Valid 
 HRA001            7/29/2012 8:17:53 AM                Check-In           Invalid
 HRA001            7/29/2012 8:18:40 AM                Check-In           Invalid
 HRA001            7/29/2012 8:18:54 AM                Check-In           Valid 
 HRA001            7/29/2012 8:18:56 AM                Check-In           Invalid
 HRA001            7/29/2012 8:18:58 AM                Check-In           Invalid
 HRA001            7/29/2012 8:19:55 AM                Check-In           Valid
 HRA001            7/29/2012 8:20:58 AM                Check-In           Valid
4

2 回答 2

1

试试这个:

;WITH users_CTE as 
(
 select *,row_number() over (partition by Username order by Checktime) as row from users
)
,CTE as(
select row,Username,Checktime,CheckType,0 as totalSeconds,'N' as Delflag from users_CTE where row=1 
union all
select t.row,t.Username,t.Checktime,t.CheckType,CASE WHEN (c.totalSeconds + DATEDIFF(SECOND,c.Checktime,t.Checktime))  >= 60 then 0 else (c.totalSeconds + DATEDIFF(SECOND,c.Checktime,t.Checktime)) end as totalSeconds,
CASE WHEN (c.totalSeconds + DATEDIFF(SECOND,c.Checktime,t.Checktime))  >= 60 then 'N' else 'Y' end as Delflag
--CASE WHEN c.totalSeconds <= 60  then 'Y' else 'N' end as Delflag
from users_CTE t inner join CTE c
on t.row=c.row+1
)

select Username,Checktime,CheckType,Delflag from CTE
于 2012-08-08T06:09:52.647 回答
0
;WITH users_CTE as 
(
 select *,row_number() over (partition by USER_NAME order by CHECKTIME) as row from daily_machine_data
WHERE Convert(varchar(10),CHECKTIME,101) = Convert(varchar(10),GetDate(),101) AND USER_NAME='HRA002'
)
,CTE as(
select row,USERID,USER_NAME,CHECKTIME,CHECKTYPE,VERIFYCODE,CHK_DATE,CHK_TIME,IP_ADDRESS,MACHINE_NO,0 as totalSeconds,'N' as Delflag from users_CTE where row=1 
union all
select t.row,t.USERID,t.USER_NAME,t.CHECKTIME,t.CHECKTYPE,t.VERIFYCODE,t.CHK_DATE,t.CHK_TIME,t.IP_ADDRESS,t.MACHINE_NO,
CASE WHEN (c.totalSeconds + DATEDIFF(SECOND,c.CHECKTIME,t.CHECKTIME))  >= 60 then 0 else (c.totalSeconds + DATEDIFF(SECOND,c.CHECKTIME,t.CHECKTIME)) end as totalSeconds,
CASE WHEN (c.totalSeconds + DATEDIFF(SECOND,c.CHECKTIME,t.CHECKTIME))  >= 60 then 'N' else 'Y' end as Delflag
--CASE WHEN c.totalSeconds <= 60  then 'Y' else 'N' end as Delflag
from users_CTE t inner join CTE c
on t.row=c.row+1
)
INSERT INTO TempMachineData(USERID,USER_NAME,CHECKTIME,CHECKTYPE,VERIFYCODE,CHK_DATE,CHK_TIME,IP_ADDRESS,MACHINE_NO)
select USERID,USER_NAME,CHECKTIME,CHECKTYPE,VERIFYCODE,CHK_DATE,CHK_TIME,IP_ADDRESS,MACHINE_NO from CTE
WHERE Delflag='N'

DELETE FROM daily_machine_data 
WHERE USER_NAME = 'HRA002' AND Convert(varchar(10),CHECKTIME,101) = Convert(varchar(10),GetDate(),101)

INSERT INTO daily_machine_data(USERID,USER_NAME,CHECKTIME,CHECKTYPE,VERIFYCODE,CHK_DATE,CHK_TIME,IP_ADDRESS,MACHINE_NO)
select USERID,USER_NAME,CHECKTIME,CHECKTYPE,VERIFYCODE,CHK_DATE,CHK_TIME,IP_ADDRESS,MACHINE_NO from TempMachineData

DELETE FROM TempMachineData
于 2012-08-08T10:58:58.100 回答