Without knowing too much about the details of your application, I would say the best way for you to structure your data is to have each record of your table correspond to a single "Punch In / Punch Out" for each employee. From there, it would be pretty trivial to calculate the hours worked for each employee over a given period of time. I'm not mistaken, that sounds pretty much like how you've describe the data to be laid out in your second example (minus the Status
column).
In the most simple example, your table would have the following columns:
UID (INT or uniqueidentifier)
ClockIn (datetime)
ClockOut (datetime)
When an employee clocked in, you would insert a row into the table for that employee and put the clock in time under the ClockIn
column.
UID ClockIn ClockOut
-------------------------------------------
1 2012-10-30 12:00:00.000 NULL
As each employee clocks in, they will each be given a row
UID ClockIn ClockOut
-------------------------------------------
1 2012-10-30 12:00:00.000 NULL
2 2012-10-30 1:00:00.000 NULL
3 2012-10-30 2:00:00.000 NULL
When an employee clocks out, your application should retrieve the existing record for that employee, and UPDATE
the row by setting the ClockOut
column to the clock out time.
UID ClockIn ClockOut
-------------------------------------------
1 2012-10-30 12:00:00.000 NULL
2 2012-10-30 1:00:00.000 2012-10-30 3:30:00.000
3 2012-10-30 2:00:00.000 NULL
If an employee clocks out and then returns later, they will be given a new record just as if they were clocking in earlier that day.
UID ClockIn ClockOut
-------------------------------------------
1 2012-10-30 12:00:00.000 NULL
2 2012-10-30 1:00:00.000 2012-10-30 3:30:00.000
3 2012-10-30 2:00:00.000 NULL
2 2012-10-30 4:30:00.000 NULL
Hopefully, at this point you should be able to see how this rather simple data model is flexible enough to allow for most of your standard time-keeping functionality.
- You can determine who is clocked in by looking for rows where
ClockOut
is NULL
- You can have some sort of process for handling when empoyees forget to punch out
- Editing punches for employees that clock in or out late.
- You can determine the total hours worked for each employee by doing a
DATEDIFF
of ClockOut
and ClockIn
for each of their rows, and summing the results
In this example, most of the responsibility for validating the data would take place in your application later. You wouldn't want someone to be clocked in multiple times, or try to clock out if they haven't actually punched in yet.