I have a table to record staffs enter and exit times. My Table:
CREATE TABLE [dbo].[tbl_attendanceSheet] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[memberCode] NVARCHAR (20) NULL,
[name] NVARCHAR (20) NULL,
[date] date NULL,
[clockin] time NULL,
[clockout] time NULL,
[delay] time NULL,
[HouresWorked] time NULL,
[desc] NVARCHAR (150) NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
I want to implement this logic:
IF ClockIn exists and ClockOut exists, Insert the value into ClockIn(new row).
IF ClockIn doesnt exist and ClockOut doesnt exist, Insert the value into ClockIn.
IF ClockIn exists and ClockOut doesnt exist, Insert the value into ClockOut.
I can implement inserting clock in and clock out without checking each other, my problem is how to check if one exists and if so, the other one should be updated if necessary.
What I tried myself:
CREATE PROCEDURE InputClock
@QmemberCode nvarchar(20),
@InputName nvarchar(20),
@InputDate Date,
@InputTime time
as
IF NOT EXISTS (SELECT TOP 1 clockin FROM tbl_attendanceSheet WHERE memberCode=@QmemberCode ORDER BY ClockIn DESC) AND NOT EXISTS (SELECT TOP 1 clockout FROM tbl_attendanceSheet WHERE memberCode=@QmemberCode ORDER BY ClockOut DESC)
BEGIN
Insert Into tbl_attendanceSheet(memberCode,name,date,clockIn) Values (@QmemberCode,@InputName,@InputDate,@InputTime)
END
ELSE IF EXISTS (SELECT TOP 1 clockin FROM tbl_attendanceSheet WHERE memberCode=@QmemberCode ORDER BY ClockIn DESC) AND NOT EXISTS (SELECT TOP 1 clockout FROM tbl_attendanceSheet WHERE memberCode=@QmemberCode ORDER BY ClockOut DESC)
BEGIN
Insert Into tbl_attendanceSheet(memberCode,name,date,clockout) Values (@QmemberCode,@InputName,@InputDate,@InputTime)
END
It doesnt work because, when clockIn column has a value and clockOut column doesnt it does not enter the time value in the clockOut column.
Why do I need this logic? Because there are several employees who would clock in, in the morning in any order and would not clock out at night in the same order. So the system should figure it out who has clockedIn before, to Clock it out and who has not done anything before to clock it in.
what I want presented in another way: lets assume this is my table:
[memberCode] | [name] | [ClockIn]| [ClockOut]
In the morning Alex comes and clocks in
[memberCode] | [name] | [ClockIn]| [ClockOut]
121 Alex 09:00:00
Jaimy comes and clocks in
[memberCode] | [name] | [ClockIn]| [ClockOut]
121 Alex 09:00:00
122 jamy 09:00:20
Neo comes and clocks in
[memberCode] | [name] | [ClockIn]| [ClockOut]
121 alex 09:00:00
122 jamy 09:00:20
123 Neo 09:00:25
Neo Leaves:
[memberCode] | [name] | [ClockIn]| [ClockOut]
121 alex 09:00:00
122 jamy 09:00:20
123 Neo 09:00:25 13:00:00
Neo Comes
[memberCode] | [name] | [ClockIn]| [ClockOut]
121 Alex 09:00:00
122 Jamy 09:00:20
123 Neo 09:00:25 13:00:00
123 Neo 18:00:00
At night, Jamie leaves:
[memberCode] | [name] | [ClockIn]| [ClockOut]
121 Alex 09:00:00
122 Jamy 09:00:20 22:00:00
123 Neo 09:00:25 13:00:00
123 Neo 18:00:00
Neo Leaves:
[memberCode] | [name] | [ClockIn]| [ClockOut]
121 Alex 09:00:00
122 Jamy 09:00:20 22:00:00
123 Neo 09:00:25 13:00:00
123 Neo 18:00:00 23:00:00
Alex Leaves:
[memberCode] | [name] | [ClockIn]| [ClockOut]
121 Alex 09:00:00 00:00:00
122 Jamy 09:00:20 22:00:00
123 Neo 09:00:25 13:00:00
123 Neo 18:00:00 23:00:00
Ofcource I need a date column as well but I omited it due to simplicity. This is my idea for an attendance sheet. If you want to add something to make it better, I would appreciate it.