Solved

Self Join query to ID a pattern fo data Needed

Posted on 2009-04-01
5
244 Views
Last Modified: 2012-05-06
I have a table that has three fields.
acct_nbr
theID
datetime to the second.

I need a query that will allow me to find a pattern where sequential acct_nbr's numbers have been accessed by the same theID based on datetime to the second. Listed below is an example of the data. It does not represent the pattern I am trying to find.  While I can sort by acct_nbr,datetime, and theID I can not bring the required pattern to the forefront.  There are 20 millions records in the table.

All help is greatly appreciated.

Data looks like
acct_nbr                     datetime                                      theID
123911111      2007-12-28 23:01:54.000      206936
133333333      2007-12-28 22:19:15.000      969023
644444444      2007-12-28 23:00:28.000      999633
155555555      2007-12-29 08:10:40.000      590635

0
Comment
Question by:frogman22
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 24041985
So the access has to take place during the SAME second?  Your requirement is unclear:
"sequential acct_nbr's numbers have been accessed by the same theID based on datetime to the second"


;with AccountsAccessed as
(select theid,[datetime] as tDate,acct_nbr
from SomeTable b
join
(select theid,[datetime]
from SomeTable
group by theid,[datetime]
having count(*)>1
)a
on a.theid=b.theid and b.[datetime]=c.[datetime]
)
select * from AccountsAccessed a1
join AccountsAccessed a2
on a1.theid=a2.theid
and a1.tdate=a2.tdate
and a1.acct_nbr=a2.acct_nbr-1

0
 

Author Comment

by:frogman22
ID: 24042365
Sorry for the confusion. I meant "to the second "in regards to sequential order.  Does the query sample need to be modified
0
 
LVL 27

Expert Comment

by:Chris Luttrell
ID: 24042595
Try this

select a1.theid, a1.acct_nbr, a2.acct_nbr, a1.[datetime]
from YourTable a1 inner join YourTable a2
   on a1.theid = a2.theid and a1.[datetime] = a2.[datetime] and a1.acct_nbr = a2.acct_nbr+1
0
 
LVL 27

Accepted Solution

by:
Chris Luttrell earned 500 total points
ID: 24042611
based on your last post this should work

select a1.theid, a1.acct_nbr, a2.acct_nbr, a1.[datetime]
from YourTable a1 inner join YourTable a2
   on a1.theid = a2.theid and a1.[datetime] = a2.[datetime]
0
 

Author Comment

by:frogman22
ID: 24042962
It is running. We will see what the results are:-)
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question