Hi @Sam
we can see same user for same day he logged in 2 times, but I want to consider only 2nd row because it was old and first logged in by user.
If I understand correctly, you want to delete one user's record within the same day except for the first login.
Please check this sample:
CREATE TABLE #TEST (ID VARCHAR(20),Name VARCHAR(20),Store_Name VARCHAR(20),Login_Hrs decimal(6,2),first_checkin DATETIME)
INSERT INTO #TEST VALUES
('AB409123','ABC','XYZ',0.02,'3/23/2023 21:07'),
('AB409123','ABC','XYZ',0,'3/23/2023 2:27'),
('AB409123','ABC','XYZ',4.55,'3/17/2023 11:09'),
('AB409123','ABC','XYZ',7.55,'3/16/2023 11:48'),
('AB409124','CBA','XXA',0.67,'3/15/2023 11:25'),
('AB409125','CBD','XXA',0,'3/13/2023 11:10')
;WITH CTE AS
(
SELECT *,ROW_NUMBER() OVER (PARTITION BY ID,CONVERT(DATE,first_checkin) ORDER BY first_checkin) AS RowNum
FROM #TEST
)
SELECT * FROM CTE --if the result is right, then comment this line and uncomment the DELETE line below.
--DELETE FROM CTE WHERE RowNum > 1;
DROP TABLE #TEST
Best regards,
Cosmog Hong
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.