counting the number of consecutive number in multiple rows.

Moderator: NorbertKrupa

Post Reply
kyle
Newbie
Newbie
Posts: 13
Joined: Fri Mar 08, 2013 4:05 am

counting the number of consecutive number in multiple rows.

Post by kyle » Wed Nov 13, 2013 8:11 pm

Hi,

I want to count the number of consecutive int value spread through out multiple rows. For example.

Code: Select all

=> select * from kyle order by value;
 value
-------
     1
     2
     5
     6
     7
    10
    12
    13
(8 rows)
In the table, I want to count the number of consecutive value tuple
- (1,2) (5,6,7) (12,13)
with the final output as the number of consecutive int in a tuple - (1,2) and (12,13) has two int. (5,6,7) has 3 int.

Code: Select all

# of consecutive int | count
2                             | 2
3                             | 1
Can I do this without using UDF? thx.

User avatar
JimKnicely
Site Admin
Site Admin
Posts: 1825
Joined: Sat Jan 21, 2012 4:58 am
Contact:

Re: counting the number of consecutive number in multiple ro

Post by JimKnicely » Wed Nov 13, 2013 10:12 pm

Here's a query that might help ...

Code: Select all

SELECT p2 as "# of consecutive int", count(p2)
  FROM (SELECT COUNT(p1) p2
          FROM (SELECT (CASE
                          WHEN a_group = 0 AND LEAD(a_group) OVER (ORDER BY value) = 1 THEN
                            conditional_change_event(a_group) OVER (ORDER BY value) + 1
                          ELSE conditional_change_event(a_group) OVER (ORDER BY value)
                        END) p1
                  FROM (SELECT value, DECODE(value - LAG(value) OVER (ORDER BY value), 1, 1, 0) a_group
                          FROM kyle) foo) foo
         WHERE p1 > 0
         GROUP BY p1
        HAVING COUNT(p1) > 1) foo
 GROUP BY p2
 ORDER BY p2;
Example:

Code: Select all

dbadmin=> SELECT * FROM kyle ORDER BY 1;
 value
-------
     1
     2
     5
     6
     7
    10
    12
    13
(8 rows)

dbadmin=> SELECT p2 as "# of consecutive int"
dbadmin->      , count(p2)
dbadmin->   FROM (SELECT COUNT(p1) p2
dbadmin(>           FROM (SELECT (CASE
dbadmin(>                           WHEN a_group = 0 AND LEAD(a_group) OVER (ORDER BY value) = 1 THEN
dbadmin(>                             conditional_change_event(a_group) OVER (ORDER BY value) + 1
dbadmin(>                           ELSE conditional_change_event(a_group) OVER (ORDER BY value)
dbadmin(>                         END) p1
dbadmin(>                   FROM (SELECT value
dbadmin(>                              , DECODE(value - LAG(value) OVER (ORDER BY value), 1, 1, 0) a_group
dbadmin(>                           FROM kyle) foo) foo
dbadmin(>          WHERE p1 > 0
dbadmin(>          GROUP BY p1
dbadmin(>         HAVING COUNT(p1) > 1) foo
dbadmin->  GROUP BY p2
dbadmin->  ORDER BY p2;
 # of consecutive int | count
----------------------+-------
                    2 |     2
                    3 |     1
(2 rows)
Jim Knicely

Image

Note: I work for Vertica. My views, opinions, and thoughts expressed here do not represent those of my employer.

id10t
GURU
GURU
Posts: 732
Joined: Mon Apr 16, 2012 2:44 pm

Re: counting the number of consecutive number in multiple ro

Post by id10t » Wed Nov 13, 2013 11:36 pm

Hi!

[DELETED]
Last edited by id10t on Sat May 09, 2015 3:26 pm, edited 1 time in total.

kyle
Newbie
Newbie
Posts: 13
Joined: Fri Mar 08, 2013 4:05 am

Re: counting the number of consecutive number in multiple ro

Post by kyle » Thu Nov 14, 2013 2:33 am

Hi sKwa and knicely87,

Both suggestions work like charm :D Thank you very much!!

Post Reply

Return to “Vertica SQL”