July 20, 2017 at 6:43 pm
Hi,
I want to calculate the difference time(minutes) between each I and O flag.
Please refer below table
EmpId | Flag | PunchedTime | AttendanceDate |
A | I | 9:16:00 AM | 7/13/2017 |
A | O | 9:48:00 AM | 7/13/2017 |
A | I | 11:06:00 AM | 7/13/2017 |
A | O | 11:38:00 AM | 7/13/2017 |
A | I | 12:38:00 PM | 7/13/2017 |
A | O | 1:00:00 PM | 7/13/2017 |
A | I | 2:07:00 PM | 7/13/2017 |
A | O | 4:42:00 PM | 7/13/2017 |
A | I | 5:27:00 PM | 7/13/2017 |
A | O | 5:42:00 PM | 7/13/2017 |
A | I | 6:25:00 PM | 7/13/2017 |
A | O | 6:26:00 PM | 7/13/2017 |
A | I | 6:31:00 PM | 7/13/2017 |
A | O | 6:32:00 PM | 7/13/2017 |
B | I | 9:16:00 AM | 7/12/2017 |
B | O | 9:48:00 AM | 7/12/2017 |
B | I | 11:06:00 AM | 7/12/2017 |
B | O | 11:38:00 AM | 7/12/2017 |
B | I | 12:38:00 PM | 7/12/2017 |
B | O | 1:00:00 PM | 7/12/2017 |
B | I | 2:07:00 PM | 7/12/2017 |
B | O | 4:42:00 PM | 7/12/2017 |
B | I | 5:27:00 PM | 7/12/2017 |
B | O | 5:42:00 PM | 7/12/2017 |
B | I | 6:25:00 PM | 7/12/2017 |
B | O | 6:26:00 PM | 7/12/2017 |
B | I | 6:31:00 PM | 7/12/2017 |
B | O | 6:32:00 PM | 7/12/2017 |
July 20, 2017 at 7:48 pm
Thanks for the data sample... however, that's not super helpful. (Imagine copying and pasting that into SSMS and trying to run it.) Here's how to do it:
CREATE TABLE #TimeClock(
EmpID CHAR,
ClockType CHAR,
StampTime TIME,
StampDate DATE
);
GO
INSERT INTO #TimeClock VALUES
('A', 'I', '6:31:00 PM', '7/13/2017'),
('A', 'O', '6:32:00 PM', '7/13/2017'),
('B', 'I', '9:16:00 AM', '7/12/2017'),
('B', 'O', '9:48:00 AM', '7/12/2017'),
('B', 'I', '11:06:00 AM', '7/12/2017'),
('B', 'O', '11:38:00 AM', '7/12/2017'),
('B', 'I', '12:38:00 PM', '7/12/2017'),
('B', 'O', '1:00:00 PM', '7/12/2017'),
('B', 'I', '2:07:00 PM', '7/12/2017'),
('B', 'O', '4:42:00 PM', '7/12/2017');
Then folks here can just run the code and start from where you are.
Since you're new, please read Jeff Moden's article explaining Best Practices... it's a great read on how to post questions so that they get answered.
Here's a hack answer... (requires 2012 or later):SELECT tc.EmpID
, tc.StampDate
, tc.StampTime
, DATEDIFF(n,tc.PrevEvent,tc.StampTime) AS elapsed
, tc.EventDate
, tc.ClockType
, tc.PrevEvent
FROM
(SELECT EmpID
, StampDate
, StampTime
, CAST(CAST(StampDate AS VARCHAR(10)) + ' ' + CAST(StampTime AS VARCHAR(10)) AS DATETIME) EventDate
, ClockType
, ROW_NUMBER() OVER (PARTITION BY EmpID, StampDate ORDER BY StampTime) AS ClockID
, CASE WHEN ClockType = 'O' THEN LAG(StampTime) OVER (PARTITION BY EmpID, StampDate ORDER BY StampTime) END PrevEvent
FROM #TimeClock) tc
WHERE tc.ClockType = 'O';
July 21, 2017 at 5:35 am
I'll also ask the question of, is there any guarantee that for every "I", there will be a related "O" and vice versa?
--Jeff Moden
Change is inevitable... Change for the better is not.
July 21, 2017 at 7:29 am
Yes it will have confirmed each I and O
July 21, 2017 at 9:12 am
pietlinden - Thursday, July 20, 2017 7:48 PMThanks for the data sample... however, that's not super helpful. (Imagine copying and pasting that into SSMS and trying to run it.) Here's how to do it:
CREATE TABLE #TimeClock(
EmpID CHAR,
ClockType CHAR,
StampTime TIME,
StampDate DATE
);
GOINSERT INTO #TimeClock VALUES
('A', 'I', '6:31:00 PM', '7/13/2017'),
('A', 'O', '6:32:00 PM', '7/13/2017'),
('B', 'I', '9:16:00 AM', '7/12/2017'),
('B', 'O', '9:48:00 AM', '7/12/2017'),
('B', 'I', '11:06:00 AM', '7/12/2017'),
('B', 'O', '11:38:00 AM', '7/12/2017'),
('B', 'I', '12:38:00 PM', '7/12/2017'),
('B', 'O', '1:00:00 PM', '7/12/2017'),
('B', 'I', '2:07:00 PM', '7/12/2017'),
('B', 'O', '4:42:00 PM', '7/12/2017');Then folks here can just run the code and start from where you are.
Since you're new, please read Jeff Moden's article explaining Best Practices... it's a great read on how to post questions so that they get answered.
Here's a hack answer... (requires 2012 or later):SELECT tc.EmpID
, tc.StampDate
, tc.StampTime
, DATEDIFF(n,tc.PrevEvent,tc.StampTime) AS elapsed
, tc.EventDate
, tc.ClockType
, tc.PrevEvent
FROM
(SELECT EmpID
, StampDate
, StampTime
, CAST(CAST(StampDate AS VARCHAR(10)) + ' ' + CAST(StampTime AS VARCHAR(10)) AS DATETIME) EventDate
, ClockType
, ROW_NUMBER() OVER (PARTITION BY EmpID, StampDate ORDER BY StampTime) AS ClockID
, CASE WHEN ClockType = 'O' THEN LAG(StampTime) OVER (PARTITION BY EmpID, StampDate ORDER BY StampTime) END PrevEvent
FROM #TimeClock) tc
WHERE tc.ClockType = 'O';
I don't know why you call it a hack. It's supported by MS and is more efficient than the previous standard method of getting data from a "previous" row.
Also, converting from date/time data to (n)(var)char data and back is expensive. I much prefer the following approach.SELECT StampDate, StampTime, DATEADD(DAY, DATEDIFF(DAY, 0, StampDate), CAST(StampTime AS DATETIME))
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply