Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL count consecutive rows

I have the following data in a table:

|event_id    |starttime        |person_id|attended|
|------------|-----------------|---------|--------|
| 11512997-1 | 01-SEP-16 08:00 | 10001   | N      |
| 11512997-2 | 01-SEP-16 10:00 | 10001   | N      |
| 11512997-3 | 01-SEP-16 12:00 | 10001   | N      |
| 11512997-4 | 01-SEP-16 14:00 | 10001   | N      |
| 11512997-5 | 01-SEP-16 16:00 | 10001   | N      |
| 11512997-6 | 01-SEP-16 18:00 | 10001   | Y      |
| 11512997-7 | 02-SEP-16 08:00 | 10001   | N      |
| 11512997-1 | 01-SEP-16 08:00 | 10002   | N      |
| 11512997-2 | 01-SEP-16 10:00 | 10002   | N      |
| 11512997-3 | 01-SEP-16 12:00 | 10002   | N      |
| 11512997-4 | 01-SEP-16 14:00 | 10002   | Y      |
| 11512997-5 | 01-SEP-16 16:00 | 10002   | N      |
| 11512997-6 | 01-SEP-16 18:00 | 10002   | Y      |
| 11512997-7 | 02-SEP-16 08:00 | 10002   | Y      |

I want to produce the following results, where the maximum number of consecutive occurences where atended = 'N' is returned:

|person_id|consec_missed_max|
| 1001    | 5               |
| 1002    | 3               |

How could this be done in Oracle (or ANSI) SQL? Thanks!

Edit:

So far I have tried:

WITH t1 AS
(SELECT t.person_id,
    row_number() over(PARTITION BY t.person_id ORDER BY t.starttime) AS idx
    FROM the_table t
    WHERE t.attended = 'N'),
t2 AS
(SELECT person_id, MAX(idx) max_idx FROM t1 GROUP BY person_id)
SELECT t1.person_id, COUNT(1) ct
    FROM t1
    JOIN t2
    ON t1.person_id = t2.person_id
GROUP BY t1.person_id;
like image 697
ubersnack Avatar asked Sep 23 '16 15:09

ubersnack


1 Answers

The main work is in the factored subquery "prep". You seem to be somewhat familiar with analytic function, but that is not enough. This solution uses the so-called "tabibitosan" method to create groups of consecutive rows with the same characteristic in one or more dimensions; in this case, you want to group consecutive N rows with a different group for each sequence. This is done with a difference of two ROW_NUMBER() calls - one partitioned by person only, and the other by person and attended. Google "tabibitosan" to read more about the idea if needed.

with
     inputs ( event_id, starttime, person_id, attended ) as (
        select '11512997-1', to_date('01-SEP-16 08:00', 'dd-MON-yy hh24:mi'), 10001, 'N' from dual union all
        select '11512997-2', to_date('01-SEP-16 10:00', 'dd-MON-yy hh24:mi'), 10001, 'N' from dual union all     
        select '11512997-3', to_date('01-SEP-16 12:00', 'dd-MON-yy hh24:mi'), 10001, 'N' from dual union all
        select '11512997-4', to_date('01-SEP-16 14:00', 'dd-MON-yy hh24:mi'), 10001, 'N' from dual union all
        select '11512997-5', to_date('01-SEP-16 16:00', 'dd-MON-yy hh24:mi'), 10001, 'N' from dual union all
        select '11512997-6', to_date('01-SEP-16 18:00', 'dd-MON-yy hh24:mi'), 10001, 'Y' from dual union all
        select '11512997-7', to_date('02-SEP-16 08:00', 'dd-MON-yy hh24:mi'), 10001, 'N' from dual union all
        select '11512997-1', to_date('01-SEP-16 08:00', 'dd-MON-yy hh24:mi'), 10002, 'N' from dual union all
        select '11512997-2', to_date('01-SEP-16 10:00', 'dd-MON-yy hh24:mi'), 10002, 'N' from dual union all
        select '11512997-3', to_date('01-SEP-16 12:00', 'dd-MON-yy hh24:mi'), 10002, 'N' from dual union all
        select '11512997-4', to_date('01-SEP-16 14:00', 'dd-MON-yy hh24:mi'), 10002, 'Y' from dual union all
        select '11512997-5', to_date('01-SEP-16 16:00', 'dd-MON-yy hh24:mi'), 10002, 'N' from dual union all
        select '11512997-6', to_date('01-SEP-16 18:00', 'dd-MON-yy hh24:mi'), 10002, 'Y' from dual union all
        select '11512997-7', to_date('02-SEP-16 08:00', 'dd-MON-yy hh24:mi'), 10002, 'Y' from dual
      ),
      prep ( starttime, person_id, attended, gp ) as (
        select starttime, person_id, attended,
               row_number() over (partition by person_id order by starttime) -
                   row_number() over (partition by person_id, attended 
                                      order by starttime)
        from   inputs
      ),
      counts ( person_id, consecutive_absences ) as (
        select person_id, count(*)
        from   prep
        where  attended = 'N'
        group by person_id, gp
     )
select person_id, max(consecutive_absences) as max_consecutive_absences
from   counts
group by person_id
order by person_id;

OUTPUT:

 PERSON_ID                MAX_CONSECUTIVE_ABSENCES
---------- ---------------------------------------
     10001                                       5
     10002                                       3
like image 119
mathguy Avatar answered Oct 02 '22 05:10

mathguy