dimanche 16 décembre 2018

How to check all previous values in an SQL Server table via c# or SQL code [on hold]

I have a table to record staffs enter and exit times. 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.

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)
);

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 ot 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.

Aucun commentaire:

Enregistrer un commentaire