Solved

Oracle SQL Query Count Most Consecutive Numbers

Posted on 2014-01-10
3
2,380 Views
Last Modified: 2014-01-10
CREATE TABLE EX2
   (      ID NUMBER,
      YEAR NUMBER
   );

Insert into EX2 (ID,YEAR) values (1,2010);
Insert into EX2 (ID,YEAR) values (1,2000);
Insert into EX2 (ID,YEAR) values (1,2009);
Insert into EX2 (ID,YEAR) values (1,2011);
Insert into EX2 (ID,YEAR) values (1,2012);
Insert into EX2 (ID,YEAR) values (1,2013);
Insert into EX2 (ID,YEAR) values (1,2001);
Insert into EX2 (ID,YEAR) values (1,2002);
Insert into EX2 (ID,YEAR) values (1,2003);
Insert into EX2 (ID,YEAR) values (1,2005);
Insert into EX2 (ID,YEAR) values (1,2006);

This query works fine:

select
count(*) Keep(Dense_Rank Last order by distance) as cnt
from (select id,
      Year+Row_Number() over(partition by id order by YEAR desc) as distance
        from ex2)
group by ID;

It returns 5 which is correct but if you insert a duplicate ie:
Insert into EX2 (ID,YEAR) values (1,2011);
it will not count consequtive years properly anymore.

Can you make it get over this duplicate problem and still return the 5 with that duplicate in there?
0
Comment
Question by:claghorn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
3 Comments
 
LVL 74

Expert Comment

by:sdstuber
ID: 39771568
distinct first?

select
count(*) Keep(Dense_Rank Last order by distance) as cnt
from (select id,
      Year+Row_Number() over(partition by id order by YEAR desc) as distance
        from (select distinct id,year from ex2))
group by ID;
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 39771590
no, that doesn't work either

try this..

insert into ex2
select 1, 2000 - level from dual connect by level < 10;

then run either of the above queries.  Even though there is a 13-year consecutive run, neither of the above queries can find it.


Try this...

  SELECT id, MAX(cnt)
    FROM (  SELECT id, COUNT(*) cnt
              FROM (SELECT id, year - DENSE_RANK() OVER(PARTITION BY id ORDER BY year) n
                      FROM (SELECT DISTINCT id, year FROM ex2))
          GROUP BY id, n)
GROUP BY id

you could also use row_number instead of dense_rank
0
 
LVL 74

Accepted Solution

by:
sdstuber earned 500 total points
ID: 39771873
slightly simpler version...

  SELECT id, MAX(cnt)
    FROM (  SELECT id, COUNT(DISTINCT year) cnt
              FROM (SELECT id, year, year - DENSE_RANK() OVER(PARTITION BY id ORDER BY year) n FROM ex2)
          GROUP BY id, n)
GROUP BY id

or, if you don't need the id, just whatever has the most consecutive years...

  SELECT MAX(COUNT(DISTINCT year))
    FROM (SELECT id, year, year - DENSE_RANK() OVER(PARTITION BY id ORDER BY year) n FROM ex2)
GROUP BY id, n;


note these you can't use row_number, you must use dense_rank
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sybase and replication server 13 82
oracle date format checking 7 53
subtr returning incorrect value 8 94
plsql job on oracle 18 109
How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.

734 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