Fahad Abid Janjua
Fahad Abid Janjua

Reputation: 1024

Deletion of Invalid Records in SQL on the basis of a Timeout

I have a table which stores the check-in times of employees through Time Machine on the basis of a username. If an employee punches multiple times then there would be multiple records of his check-ins. All those records which come within 1 minute of first record are invalid and must be deleted. If a record which comes more than a minute later of first record is valid and must not be deleted. Then this second valid record may also contains subsequent records which are invalid as they come within 1 min of this second valid record.Such records should also be deleted and so on. How can i do it on all the checkin records of an employee for the current date? The flag field is not in db its just a demonstration that which record is valid and which is invalid.

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

Upvotes: 1

Views: 96

Answers (2)

Fahad Abid Janjua
Fahad Abid Janjua

Reputation: 1024

;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

Upvotes: 0

AnandPhadke
AnandPhadke

Reputation: 13506

try this:

;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

Upvotes: 1

Related Questions