Home > Ask the Oracle Experts > SQL Questions & Answers > Latest transaction if no recent prior transactions
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Latest transaction if no recent prior transactions

Rudy Limeback EXPERT RESPONSE FROM: Rudy Limeback

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site


Oracle tips, scripts, and expert advice
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


>
QUESTION POSED ON: 31 August 2007

I would like to select the latest customer record only if there are not any prior transactions for the previous 180 days. My sample data is like:

CustNum     InvDate
1200        2007-05-10
1200        2007-07-10
1200        2007-08-20
1000        2006-06-01
1000        2006-07-01
1000        2007-08-10

Assuming you are running this as of 2007-08-25, the query should return only one record of customer number 1000 with the invoice date of 2007-08-10. Any help is appreciated. Thanks in advance.


>
EXPERT RESPONSE

This is a variation of the "Latest X for each Y" scenario in the SQL FAQ: Common SQL questions. The wrinkle here is that an additional NOT EXISTS condition is required.

select CustNum
     , InvDate
  from Invoices as T
 where InvDate =
       ( select max(InvDate)
           from Invoices
          where CustNum = T.CustNum )
   and not exists
       ( select *
           from Invoices
          where CustNum = T.CustNum 
            and DateSubtract(T.InvDate,InvDate)
                <= 180 )

Here, DateSubtract is a function to calculate the difference, in days, between those two dates. This function is available in every database system, but it's different in each one, so you'll have to look it up.

Notice how each of the two subqueries uses a correlation variable, namely T. This correlates the rows being examined in the subquery with the row in the outer query, as identified by the table alias T for the Invoices table in the outer query. So T.InvDate is the InvDate of the row in the outer query, which may or may not be selected for the result set, depending on whether the outer query's WHERE clause is satisfied. The other Invdate in the subquery is the InvDate of every row in the table which has the same CustNum as T.Custnum, the row in the outer query.

It does sound a bit confusing, doesn't it.

Another way to do the second subquery is:

   and 0 =
       ( select count(*)
           from Invoices
          where CustNum = T.CustNum 
            and DateSubtract(T.InvDate,InvDate)
                <= 180 )

Compare this with the NOT EXISTS subquery.


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


RELATED CONTENT
SQL
SQL to select rows 1000 through 3000 in a table
SQL query to combine rows
The SQL REPLACE function
CASE expressions in the ORDER BY clause
Finding a column value inside a user-supplied string
Update a specific column in a field or row?
Using BETWEEN with DATETIMEs in SQL
Which normal form is used most?
IN list or series of OR conditions?
Connecting tables in a database

Oracle SQL
SQL to count values of a status code
Counting NULL columns
Detail rows for accounts that occur three times
Counting a row's NULL columns
Oracle's free SQL Developer adds database migration tool
Three ways SQL can count rows by type
SQL to select only certain times within a date range
Oracle SQL to test for numerics
Number of rows in multiple tables
Stringing together columns with UPDATE SQL
Oracle SQL Research

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
autonomous transaction  (SearchOracle.com)
CFML  (SearchOracle.com)
dynamic SQL  (SearchOracle.com)
foreign key  (SearchOracle.com)
Java Database Connectivity  (SearchOracle.com)
Open Database Connectivity  (SearchOracle.com)
Oracle  (SearchOracle.com)
stored procedure  (SearchOracle.com)
The Open Group  (SearchOracle.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice

HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2003 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts