Home > Ask the Oracle Experts > SQL Questions & Answers > Detail rows for accounts that occur three times
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Detail rows for accounts that occur three times

Rudy Limeback EXPERT RESPONSE FROM: Rudy Limeback

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


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


>
QUESTION POSED ON: 05 December 2007
I have a table with weekly data:
    WeekEnding   Amt   Acct
    ----------  ----   ----
    2007-10-06  5.00   XYZ
    2007-10-13  3.00   XYZ
    2007-10-20  0.23   XYZ
    2007-10-06  5.00   ZZZ
    2007-10-13  0.23   ZZZ
I want all of the accounts and amounts for accounts that occur in at least three different week ending dates. Can you help?

>
EXPERT RESPONSE

The difficulty with this type of problem is that it requires returning detail rows for a situation which is easily solved with HAVING. However, to use HAVING we have to use GROUP BY, and then the detail rows are aggregated. The way to approach this problem is to break it down into components. The first component of the solution does involve the HAVING clause.

select Acct 
  from Weekly
group
    by Acct
having count(*) >= 3

Note that COUNT(DISTINCT WeekEnding) also works. Whether we use this or the simpler COUNT(*) depends on whether the combination of WeekEnding and Acct is unique. Counting distinct values may be necessary if, for example, there are other columns you didn't mention, such as customer. In that case, we might have one row per customer per account per week ending date, so COUNT(DISTINCT WeekEnding) is necessary if the GROUP BY is only on the account.

Now that we know which accounts qualify, we can build the other component, which is the selection of all detail rows for the accounts which qualify. This can be accomplished in two ways. The first method uses an IN subquery:

select WeekEnding
     , Amt
     , Acct 
  from Weekly
 where Acct in
       ( select Acct 
           from Weekly
         group
             by Acct
         having count(*) >= 3 )

The second method uses the same subquery but as a derived table in the FROM clause:

select WeekEnding
     , Amt
     , Acct 
  from Weekly
inner
  join ( select Acct 
           from Weekly
         group
             by Acct
         having count(*) >= 3 ) as ok_accts
    on ok_accts.Acct = Weekly.Acct

What is the difference between these two solutions? As far as execution goes, they should perform the same. But the derived table has an advantage. Suppose that if, instead of one column, the HAVING condition needed to be based on a GROUP BY involving two columns. What if you wanted details for all customer accounts that occurred at least 3 times?

The first solution would be:

select WeekEnding
     , Amt
     , Acct 
     , Customer
  from Weekly
 where ( Acct, Customer ) in
       ( select Acct 
              , Customer
           from Weekly
         group
             by Acct
              , Customer
         having count(*) >= 3 )

This solution now uses what is called a row constructor, and this is perfectly valid SQL. The problem with row constructors is that not every database engine supports them. Yet.

Now imagine what the query involving the join to the derived table would look like.


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


RELATED CONTENT
SQL
How to return a zero in SQL instead of no row back for a select count
How to write an SQL query using GROUP BY for row analysis
Using nested SQL string functions to find ERP customer values in a table
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

Oracle SQL
SQL to count values of a status code
Counting NULL columns
Counting a row's NULL columns
Oracle's free SQL Developer adds database migration tool
Latest transaction if no recent prior transactions
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