May 31, 2007 at 3:42 am
Hi,
i've a table with two columns (userid, logintime).
i want to remove the rows if the time difference between them is less than 10 seconds and keep only one row for that userid.
for example,
userid logintime
1234 2007-05-10 17:18:00
1234 2007-05-10 17:18:10
1234 2007-05-10 17:18:12
here, i want to remove the last two rows and keep only the top one.
can anyone help me on this?
May 31, 2007 at 4:07 am
Ganesh
Not sure how well this would perform, but one way of doing it would be to create a temp table with an identity column, say tempID. Create an index on the identity column. Then select all your table data into that table, ordered by userid and logintime. Your query would then look something like this:
SELECT userid, logintime
FROM #temptable t1
JOIN #temptable t2
ON t2.tempid = t1.tempid + 1
AND t1.userid = t2.userid
AND DATEDIFF(ss, t1.logintime, t2.logintime) > 10
John
June 1, 2007 at 9:15 am
No problem Ganesh -
--===== Create a test table and populate it with test data CREATE TABLE #MyHead (userid INT, logintime DATETIME) INSERT INTO #MyHead (userid, logintime)
SELECT 1234, '2007-05-10 17:18:00' UNION ALL SELECT 1234, '2007-05-10 17:18:11' UNION ALL SELECT 1234, '2007-05-10 17:18:13' UNION ALL -- SELECT 1234, '2007-05-10 17:18:15' UNION ALL --
SELECT 1234, '2007-05-10 18:18:10' UNION ALL SELECT 1234, '2007-05-10 18:18:12' UNION ALL SELECT 1234, '2007-05-10 18:18:14' UNION ALL -- SELECT 1234, '2007-05-10 18:18:16' UNION ALL --
SELECT 1234, '2007-05-10 19:18:10' UNION ALL SELECT 1234, '2007-05-10 19:18:12' UNION ALL -- SELECT 1234, '2007-05-10 19:18:14' UNION ALL -- SELECT 1234, '2007-05-10 19:18:16' UNION ALL --
SELECT 1235, '2007-05-10 17:18:00' UNION ALL SELECT 1235, '2007-05-10 17:18:11' UNION ALL SELECT 1235, '2007-05-10 17:18:13' UNION ALL -- SELECT 1235, '2007-05-10 17:18:15' UNION ALL --
SELECT 1235, '2007-05-10 18:18:10' UNION ALL SELECT 1235, '2007-05-10 18:18:12' UNION ALL SELECT 1235, '2007-05-10 18:18:14' UNION ALL -- SELECT 1235, '2007-05-10 18:18:16' UNION ALL --
SELECT 1235, '2007-05-10 19:18:10' UNION ALL SELECT 1235, '2007-05-10 19:18:12' UNION ALL -- SELECT 1235, '2007-05-10 19:18:14' UNION ALL -- SELECT 1235, '2007-05-10 19:18:16' UNION ALL --
SELECT 1236, '2007-05-10 17:18:00' UNION ALL SELECT 1236, '2007-05-10 17:18:11' UNION ALL SELECT 1236, '2007-05-10 17:18:13' UNION ALL -- SELECT 1236, '2007-05-10 17:18:15' UNION ALL --
SELECT 1236, '2007-05-10 18:18:10' UNION ALL SELECT 1236, '2007-05-10 18:18:12' UNION ALL -- SELECT 1236, '2007-05-10 18:18:14' UNION ALL -- SELECT 1236, '2007-05-10 18:18:16' UNION ALL --
SELECT 1236, '2007-05-10 19:18:02' UNION ALL SELECT 1236, '2007-05-10 19:18:12' UNION ALL -- SELECT 1236, '2007-05-10 19:18:14' UNION ALL -- SELECT 1236, '2007-05-10 19:18:16' --
-- Delete unwanted rows DELETE FROM #MyHead FROM (SELECT DISTINCT t1.userid, t1.logintime FROM #MyHead t1 INNER JOIN #MyHead t2 ON t2.userid = t1.userid AND DATEDIFF (ss, t2.logintime, t1.logintime) between 1 and 9   d WHERE d.userid = #MyHead.userid AND d.logintime = #MyHead.logintime
Cheers
ChrisM
Thanks again Jeff for the test table
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
June 1, 2007 at 4:14 pm
Heh... thanks Chris. And, nice job, by the way
But... try this... one less join = faster performance
DELETE t1
FROM #MyHead t1
INNER JOIN #MyHead t2
ON t1.UserID = t2.UserID
AND DATEDIFF (ss, t2.LoginTime, t1.LoginTime) BETWEEN 1 AND 9
--Jeff Moden
Change is inevitable... Change for the better is not.
June 4, 2007 at 3:42 am
Lovely! Nice way to end a friday.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
June 7, 2007 at 7:06 am
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply