Home > Ask the Oracle Database / Applications Experts > SQL Questions & Answers > Counting rows in runs
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Counting rows in runs

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: 29 March 2007

If I have a table with fields MarketId and XY, where MarketID is a unique character field and for this example XY can only contain "X" or "Y." My example data is this:

MarketID  XY
--------  --
1         X
2         X
3         X
4         Y
5         Y
6         X
7         X

What I need to do is count the occurrences of field XY but in MarketId order to end up with a result something like this:

XY    Count
--    -----
X       3
Y       2
X       2

Thanks for your help.



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


RELATED CONTENT
SQL
How to check SQL query construction with the Mimer Validator
Using the SQL GROUP BY clause for counting combinations
How to use an SQL CASE expression
How to sort an SQL UNION query with special ORDER BY sequence
How to use string functions to make an SQL join
An SQL solution for a customer order homework problem
How to use SQL's POSITION function with substrings
Using SQL date functions to get totals for last three days
Using CASE in the SQL ORDER BY clause
What's the difference between an SQL inner join and equijoin?

Oracle and SQL
Using the SQL GROUP BY clause for counting combinations
How to use an SQL CASE expression
How to use the Oracle Database SQL Reference Manual
How to use SQL Developer to run SQL statements
How to work with the Oracle database home page
How to use SQL*Plus in Oracle
How to use SQL Developer to work with an Oracle database
How to view and edit table column definitions
How to sort an SQL UNION query with special ORDER BY sequence
How to use string functions to make an SQL join
Oracle and 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


These are called "runs" or "sequences" and the SQL can sure be tricky.

In this table, the MarketID column provides the inherent ordering of data. Image the IDs as points on a line:

------o------o------o------o------

Now we will self-join this table:

  from Market_XY as t1
inner
  join Market_XY as t3
    on t3.MarketID > t1.MarketID
   and t3.XY = t1.XY 

We will pair up each row (t1) to all other rows (t3) that have a higher ID as well as the same XY. The same XY means these two rows are a potential run:

------o------o------o------o------
      |      |      |      |
      t1     t3---->t3---->t3---->

Next, we self-join once more, but with a LEFT OUTER JOIN, using an IS NULL condition in the WHERE clause to make sure no matches are found:

  from Market_XY as t1
inner
  join Market_XY as t3
    on t3.MarketID > t1.MarketID
   and t3.XY = t1.XY
left outer
  join Market_XY as t2
    on t2.MarketID between t1.MarketID and t3.MarketID
   and t2.XY <> t3.XY
 where t2.MarketID is null

Essentially, we want all t1-t3 runs, where there is no t2 between them with a different XY. The LEFT OUTER JOIN looks for them, but the IS NULL keeps only the t1-t3 pairs where no t2 exists.

But obviously there will be overlaps of runs. In the given sample data, notice that the run (1,2,3) of Xs, has runs (1,2) and (2,3) inside it. What remains now is simply to take the longest runs. This involves careful grouping and the use of both MIN() and MAX().

select XY
     , hiID - min(loID) + 1 as Count
  from (
       select t1.MarketID as loID
            , t1.XY
            , max(t3.MarketID) as hiID
         from Market_XY as t1
       inner
         join Market_XY as t3
           on t3.MarketID > t1.MarketID
          and t3.XY = t1.XY
       left outer
         join Market_XY as t2
           on t2.MarketID
              between t1.MarketID
                  and t3.MarketID
          and t2.XY <> t3.XY
        where t2.MarketID is null
       group
           by t1.MarketID
            , t1.XY
       ) as hiIDs
group
    by XY
     , hiID
order
    by hiID

It looks tricky but it really isn't. To understand it, run the subquery alone, without the MAX() or GROUP BY, but showing the t3 columns. Then see what happens with the MAX() and GROUP BY. Then apply the outer query with its GROUP BY and MIN(). Note that MAX - MIN + 1 gives the count for the run.

P.S. If anyone has an analytic SQL solution for this, please do send it in. I would love to see it.




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



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

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




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