This problem comes from an access database and old asp code. The program is basically a punch clock. People punch in and out. This is the basic guts of a punch record
The date and time are separate because it was originally put together in access and looked prettier then. It is now beind stored in SQL2000, but changing all the code would have been a task.
employeeID(int)
Date(datetime)
Type(int) : 0 is a punch in, 1 is a punch out
Time(datetime) : All records will be on 12/30/1899 only time is being inserted. Date irrelevant here
So this persons basic day is
6-7, 11-12, and 2-3pm
I need to calculate the hours per "work shift".
Fiddling around I have this sp that mostly works. Its running as the exec() because I wanted to label the result sets when testing.
This is what it outputs when all their punches are correct.
I'll be tossing a sum() on it once I know it is working.
The problem is that managers can come in and edit punches, delete them and what not.
Example:
If the manager deletes the 7am punch. Only the workshifts of 11-12 and 2-3 should return.
If they delete the 3pm punch only the first two (6-7,11-12)
Unfortunatley this is what is returned in the 7am case.
The 360 line should not display at all. As you can see it is turning the shift into
6-12, 11-12, and 2-3
If the last punch of the day is missing, everything works well.
I was trying to get it to work using Join's, but I can't seem to get the Top 1 tool to work with it.
Any help would be VERY much appreciated
The date and time are separate because it was originally put together in access and looked prettier then. It is now beind stored in SQL2000, but changing all the code would have been a task.
employeeID(int)
Date(datetime)
Type(int) : 0 is a punch in, 1 is a punch out
Time(datetime) : All records will be on 12/30/1899 only time is being inserted. Date irrelevant here
Code:
employeeID Date Type Time
---------- ------------------------ ------ -----------------------
22 2004-09-08 00:00:00.000 0 1899-12-30 06:00:00.000
22 2004-09-08 00:00:00.000 1 1899-12-30 7:00:00.000
22 2004-09-08 00:00:00.000 0 1899-12-30 11:00:00.000
22 2004-09-08 00:00:00.000 1 1899-12-30 12:00:00.000
22 2004-09-08 00:00:00.000 0 1899-12-30 14:00:00.000
22 2004-09-08 00:00:00.000 1 1899-12-30 15:00:00.000
So this persons basic day is
6-7, 11-12, and 2-3pm
I need to calculate the hours per "work shift".
Fiddling around I have this sp that mostly works. Its running as the exec() because I wanted to label the result sets when testing.
Code:
CREATE PROCEDURE GetEmpTime
@empid varchar(9),
@date datetime,
@Message varchar(20)
AS
EXEC("SELECT DATEDIFF(mi, PP.Time, PQ.Time) AS " + @Message + ", PP.Time, PQ.Time
FROM Punch PP, Punch PQ
WHERE
PP.Type=0 AND PP.employeeID=" + @empid + " AND PP.Date='" + @date + "' AND
PQ.Type=1 AND PQ.employeeID=" + @empid + " AND PQ.Date='" + @date + "' AND
PQ.Time IN
(SELECT S2.Time
FROM
(SELECT EmployeeID, Date, Type, Time
FROM Punch P1
WHERE Type=0 AND employeeID=" + @empid + " AND Date='" + @date + "') AS S1,
(SELECT Top 1 EmployeeID, Date, Type, Time
FROM Punch P1
WHERE Type=1 AND employeeID=" + @empid + " AND Date='" + @date + "' AND PP.Time< P1.Time) AS S2)");
GO
This is what it outputs when all their punches are correct.
Code:
Time Time
---------- ------------------------ ------------------------
60 1899-12-30 06:00:00.000 1899-12-30 07:00:00.000
60 1899-12-30 11:00:00.000 1899-12-30 12:00:00.000
60 1899-12-30 14:00:00.000 1899-12-30 15:00:00.000
I'll be tossing a sum() on it once I know it is working.
The problem is that managers can come in and edit punches, delete them and what not.
Example:
If the manager deletes the 7am punch. Only the workshifts of 11-12 and 2-3 should return.
If they delete the 3pm punch only the first two (6-7,11-12)
Unfortunatley this is what is returned in the 7am case.
Code:
dddd Time Time
----- ------------------------ ------------------------
360 1899-12-30 06:00:00.000 1899-12-30 12:00:00.000
60 1899-12-30 11:00:00.000 1899-12-30 12:00:00.000
60 1899-12-30 14:00:00.000 1899-12-30 15:00:00.000
The 360 line should not display at all. As you can see it is turning the shift into
6-12, 11-12, and 2-3
If the last punch of the day is missing, everything works well.
I was trying to get it to work using Join's, but I can't seem to get the Top 1 tool to work with it.
Any help would be VERY much appreciated