I am doing a project called "Attendance Management System"
The company has its own biometric finger scanner who checks for employee's attendance and stored in on a database table.
The table consist of 4 fields (ID autonumber, EmployeeNo, CheckTime and CheckType)
The Timekeeping system store all the employee's logs and attendance but it doesn't specify whether the checktype is "I" or "O". As you can see the image below, everytime the employee log in, the system store it to database automatically but it stored as "I", and when the same employee log in again it will store as "I" again.
My client want it to be automated as possible. He wants to changed the checktype to "O" once the employee login again.
For example:
I am an employee. I am going to log in at today 8am, then it will store to database as
today 8AM with checktype "I".. since I am not aware that I have logged in already, I loged in again for the second time at 8:04 AM.. the system stores it as "I" again the same as if I am going to logout at 5PM, the system also record it as "I"..
I would like this to update the database automatically.Without any parameters provided by the user. I want it to be in a stored procedure to automate the process.
It should be restricted within the same employee: Example:
Employee1 logged at today 8am.. (checktype "I") Employee1 logged again at today 11am... (checktype "O") employee1 logged again at today 12pm.. (checktype "I") employee1 logged again at today 4pm (checktype "O")
Criterias should be restricted within employeeno, checktime only.. and would like to update checktype also based on given datas.
The old system is not capable storing logged ins as "O" it was purely "I"..
How to do this? I need your inputs and help badly! Please?
I have created a stored procedure but do not know where I can start it and I dont have the idea to finish the procedure and make it work..
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE AUTOMATIC_OVERRIDE
@ID INT,
@EMPLOYEENO NVARCHAR(20),
@CHECKTIME DATETIME,
@CHECKTYPE NVARCHAR(1)
AS
SET NOCOUNT ON;
WHILE @ID = (SELECT ID FROM CHECKTIME)
SELECT ID,EMPLOYEENO,CHECKTIME,CHECKTYPE
FROM CHECKTIME
GROUP BY ID,EMPLOYEENO,CHECKTIME,CHECKTYPE
ORDER BY CHECKTIME
GO
Please help me to solve this problem. Thanks.
EDIT:
The solution into the PRC:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE AutomaticOverrideByID
@ID INT,
@EMPLOYEENO NVARCHAR(20)
AS
UPDATE CHECKTIME
SET CHECKTYPE = CASE WHEN (SELECT
COUNT(ID) AS CountOfLogin
FROM CHECKTIME
WHERE CHECKTIME.EMPLOYEENO = @EMPLOYEENO
AND CHECKTIME.CheckTime < C.CheckTime) % 2 = 1 THEN 'O' ELSE CHECKTYPE END
FROM CHECKTIME C
WHERE C.ID = @ID
GO
And here is an UPDATE to the existing dataset as Dhenn asked:
UPDATE CHECKTIME
SET CHECKTYPE = CASE WHEN (SELECT
COUNT(ID) AS CountOfLogin
FROM CHECKTIME
WHERE CHECKTIME.EMPLOYEENO = C.EMPLOYEENO
AND CHECKTIME.CheckTime < C.CheckTime) % 2 = 1 THEN 'O' ELSE CHECKTYPE END
FROM CHECKTIME C
SQL Fiddle attached.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With