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)
 Finding groups of clustered data points
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

mattt
Posting Yak Master

194 Posts

Posted - 05/21/2013 :  09:31:38  Show Profile  Reply with Quote
Hi,

I have a table which records events:


create table #events
(  intRowId int identity(1,1),
   intItemId int,
   intUserId int,
   datEvent datetime)


It's a big table with many millions of rows, recording events against several thousand items and tens of thousands of users.

There's a select group of ten itemIDs I want to look for, but only when they occur in a certain pattern: I'm trying to find rows where all ten of these items have events registered against them for the same userID and close together in time, say 5 minutes.

I have absolutely NO IDEA how to go about this. One would assume partitioning is involved somewhere, but help, even just somewhere to get started, would be much appreciated.

Cheers,
Matt

James K
Flowing Fount of Yak Knowledge

3557 Posts

Posted - 05/21/2013 :  10:18:36  Show Profile  Reply with Quote
Something like this?
SELECT
	*,
	EventCount
FROM
	#Events e1
	CROSS APPLY
	(
		SELECT COUNT(*) EventCount
		FROM #Events e2
		WHERE e2.intUserId = e1.intUserId
		AND ABS(DATEDIFF(mi, e1.datEvent, e2.datEvent)) < 5
		HAVING COUNT(*) > 10
	) e2
If that is logically what you need, and if it turns out the query is very slow, it can be optimized.
Go to Top of Page

mattt
Posting Yak Master

194 Posts

Posted - 05/21/2013 :  10:33:39  Show Profile  Reply with Quote
Basically, yes, except I'm looking for ten specific items, rather than ten sequential events. But I guess that just means substituting an "in" clause for the "having"?
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3557 Posts

Posted - 05/21/2013 :  10:41:32  Show Profile  Reply with Quote
I think that may be right. Are you able to try the query on a small sample data set to see if it gives you the correct results?
Go to Top of Page

mattt
Posting Yak Master

194 Posts

Posted - 05/21/2013 :  10:44:23  Show Profile  Reply with Quote
Yes. It seems to work best if I do it stepwise - so create a temp table with just the rows corresponding to the items in my list, and then use your query as is to find users which fit the pattern. Excellent, thank you.
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.05 seconds. Powered By: Snitz Forums 2000