我有一个表格,可以根据用户名通过Time Machine存储员工的登记时间。如果员工多次打孔,那么他的签到会有多个记录。所有那些在第一次记录的1分钟内出现的记录都是无效的,必须删除。如果第一条记录超过一分钟后的记录有效,则不得删除。然后,第二个有效记录也可能包含后续记录,这些记录在第二个有效记录的1分钟内无效。此类记录也应删除,依此类推。如何在当前日期的员工的所有签到记录中执行此操作?标志字段不在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
答案 0 :(得分: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
答案 1 :(得分: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