Solved

Self Join query to ID a pattern fo data Needed

Posted on 2009-04-01
5
241 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
  • 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 26

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 26

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Set the max value for a column 7 40
Insert from Stored Procedure where some field/s > 0 7 45
Query to Add Late Tolerance 10 76
Are triggers slow? 7 12
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…

828 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