SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Complex SQL QUERY with DateDIFF
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

CostaSBD
Starting Member

5 Posts

Posted - 02/25/2013 :  10:09:44  Show Profile  Visit CostaSBD's Homepage  Reply with Quote
Hi all,

I need to script a query to discard access logs that not exceeding a predetermined margin (eg. 2 seconds). That is, if each record was made by a user on a especific date / time, I don't want to get those who its date / time does not exceed the range compared with the date / time of the previous record. I think an example is clearer.

Example:

LogIndex, UserID, Date / Time

1. 01551, 20.02.2013 17:41:45.000
2. 01551, 20.02.2013 17:41:45.900 *
3. 01551, 20.02.2013 17:41:46.150 *
4. 01551, 20.02.2013 20:41:47.000

5. 01552, 02/20/2013 17:42:45.000
6. 01552, 20.02.2013 17:42:46.000 *
7. 01552, 02/20/2013 19:45:45.000

*: Records to discard because its date / time does not exceed the margin of 2 seconds over the previous record. In the first case two records should be discarted because both not exceed this margin.


Here's the code that creates the temporary table and adds the previous records to test:

CREATE TABLE # TEMP (LogIndex int, UserID nvarchar (10), LogTime datetime)

insert into # temp select 1, '01551 ', '20 / 02/2013 17:41:45.000'
insert into # temp select 2, '01551 ', '20 / 02/2013 17:41:45.900'
insert into # temp select 3, '01551 ', '20 / 02/2013 17:41:46.150'
insert into # temp select 4, '01551 ', '20 / 02/2013 20:41:47.000'
insert into # temp select 5, '01552 ', '20 / 02/2013 17:42:45.000'
insert into # temp select 6, '01552 ', '20 / 02/2013 17:42:46.000'
insert into # temp select 7, '01552 ', '20 / 02/2013 19:45:45.000'

select * from # temp

DROP TABLE # temp

Thanks in advance!

James K
Flowing Fount of Yak Knowledge

3323 Posts

Posted - 02/25/2013 :  10:24:18  Show Profile  Reply with Quote
SELECT a.*
FROM #temp a
WHERE NOT EXISTS
(
	SELECT * FROM #temp b
	WHERE b.LogIndex = a.LogIndex-1 AND 
	DATEDIFF(ss,b.Logtime,a.Logtime) < 2
)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 02/26/2013 :  03:15:22  Show Profile  Reply with Quote

select LogIndex,UserID,LogTime
FROM
(
select *,Row_Number() over (partition by UserID,((DATEDIFF(ss,'20000101',logtime)-1)/2*2) +1 ORDER BY LogTime) AS Rn from #temp 
)t
WHERE Rn=1


output
-------------------------
LogIndex	UserID	LogTime
-----------------------------------------
1	        01551 	2013-02-20 17:41:45.000
4	        01551 	2013-02-20 20:41:47.000
5	        01552 	2013-02-20 17:42:45.000
7	        01552 	2013-02-20 19:45:45.000



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

CostaSBD
Starting Member

5 Posts

Posted - 03/12/2013 :  08:45:51  Show Profile  Visit CostaSBD's Homepage  Reply with Quote
hi all,

Thanks a lot for the answers but if i put more records for the same userId doesn't works fine:

insert into TEMP select 1, '01551', '20/02/2013 17:41:45.000' --ok
insert into TEMP select 19, '01551', '20/02/2013 17:41:45.400'
insert into TEMP select 20, '01551', '20/02/2013 17:41:45.700'
insert into TEMP select 8, '01551', '20/02/2013 17:41:45.800'
insert into TEMP select 2, '01551', '20/02/2013 17:41:45.900'
insert into TEMP select 9, '01551', '20/02/2013 17:41:45.950'
insert into TEMP select 21, '01551', '20/02/2013 17:41:46.100'
insert into TEMP select 3, '01551', '20/02/2013 17:41:46.150'
insert into TEMP select 22, '01551', '20/02/2013 17:41:46.990'
insert into TEMP select 31, '01551', '20/02/2013 17:41:47.140'--ok
insert into TEMP select 32, '01551', '20/02/2013 17:41:47.940'
insert into TEMP select 33, '01551', '20/02/2013 17:41:48.120'
insert into TEMP select 34, '01551', '20/02/2013 17:41:48.720'
insert into TEMP select 10, '01551', '20/02/2013 20:41:45.600'--ok
insert into TEMP select 11, '01551', '20/02/2013 20:41:45.900'
insert into TEMP select 4, '01551', '20/02/2013 20:41:47.000'
insert into TEMP select 35, '01551', '20/02/2013 20:41:47.100'
insert into TEMP select 36, '01551', '20/02/2013 20:41:47.600'--ok
insert into TEMP select 37, '01551', '20/02/2013 20:41:47.900'
insert into TEMP select 24, '01551', '20/02/2013 20:41:47.700'
insert into TEMP select 25, '01551', '20/02/2013 20:41:48.990'
insert into TEMP select 26, '01551', '20/02/2013 20:41:49.100'


insert into TEMP select 5, '01552', '20/02/2013 17:42:45.000'--ok
insert into TEMP select 12, '01552', '20/02/2013 17:42:45.500'
insert into TEMP select 6, '01552', '20/02/2013 17:42:46.000'
insert into TEMP select 13, '01552', '20/02/2013 17:42:46.800'
insert into TEMP select 12, '01552', '20/02/2013 17:42:45.500'
insert into TEMP select 27, '01552', '20/02/2013 17:42:46.100'
insert into TEMP select 28, '01552', '20/02/2013 17:42:46.600'
insert into TEMP select 29, '01552', '20/02/2013 19:45:45.400'--ok
insert into TEMP select 30, '01552', '20/02/2013 19:45:45.900'
insert into TEMP select 15, '01552', '20/02/2013 19:45:46.200'



insert into TEMP select 16, '01553', '20/02/2013 19:45:45.100'--ok
insert into TEMP select 17, '01553', '20/02/2013 19:45:45.600'
insert into TEMP select 18, '01553', '20/02/2013 23:45:45.000'--ok
insert into TEMP select 19, '01553', '20/02/2013 23:45:45.200'

with this records i have 8 rows to show

How can i do it?

thanks for advance
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000