I have crosschex database that is used to store logged in/out data. Sometimes it happens that user clock in/out few times. I would like to remove that duplicates leaving the earliest when log in and latest when log out.
Here is what I have:
USE crosschex;
SELECT UI.Name, INOUT.UserId, INOUT.CheckTime, S.StatusText
FROM dbo.Checkinout AS INOUT
INNER JOIN dbo.Userinfo AS UI ON
INOUT.Userid = UI.Userid
INNER JOIN dbo.Status AS S ON INOUT.CheckType = S.Statusid
WHERE
UI.Groupid = 1 AND INOUT.CheckTime >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE())-3, 0) AND INOUT.CheckTime < DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE())-3, 1)
Here is the result:
"Zeab Simon" "6" "2018-05-04 07:58:06.000" "In"
"Zeab Simon" "6" "2018-05-04 07:58:15.000" "In"
"Zeab Simon" "6" "2018-05-04 07:58:38.000" "In"
"Kate B" "4" "2018-05-04 14:15:45.000" "In"
"Kate B" "4" "2018-05-04 14:15:46.000" "In"
"Zeab Simon" "6" "2018-05-04 16:34:46.000" "Out"
"Zeab Simon" "6" "2018-05-04 16:34:48.000" "Out"
"Zeab Simon" "6" "2018-05-04 16:34:49.000" "Out"
"Zeab Simon" "6" "2018-05-04 16:34:50.000" "Out"
"Zeab Simon" "6" "2018-05-04 16:34:51.000" "Out"
"Patty" "5" "2018-05-04 20:31:36.000" "Out"
What I would like is to remove the earliest and lattest for each userId and StatusText In/Oou to get that:
"Zeab Simon" "6" "2018-05-04 07:58:06.000" "In"
"Kate B" "4" "2018-05-04 14:15:45.000" "In"
"Zeab Simon" "6" "2018-05-04 16:34:51.000" "Out"
"Patty" "5" "2018-05-04 20:31:36.000" "Out"
How can I do it?
Best Answer
To answer this question, I did the following.
I then ran the following query:
Which gives:
A cursory inspection reveals that these are the correct times - earliest clock_in and latest clock_out. A db-fiddle is available here. To get the best answers to your questions, you might consider giving a fiddle in the question itself - help us to help you!
Following the OP's clarifications in comments the need to delete extraneous records from the same table, I used the result above in a
CTE
(Common Table Expression - AKA theWITH
clause) to delete the other records from the main shift table as follows.And then a simple
SELECT
in the fiddle shows that the correct result has been achieved.Another solution was kindly provided by @ypercube here - worth looking at for learning purposes. It is actually (no surprise here) more correct. My SQL will not work properly if there are exactly matching times for a
MAX
(orMIN
) and a non significant value!