Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Oracle SQL Query Count Most Consecutive Numbers

Posted on 2014-01-10
3
Medium Priority
?
2,457 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

704 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