Solved

Self Join query to ID a pattern fo data Needed

Posted on 2009-04-01
5
238 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
It is running. We will see what the results are:-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now