Select only Contiguous Records in DB2 SQL

146 views Asked by At

So i have a table of readings (heavily simplified version below) - sometimes there is a break in the reading history (see the record i have flagged as N) - The 'From Read' should always match a previous 'To Read' or the 'To Read' should always match a later 'From Read' BUT I want to only select records as far back as the first 'break' in the reads.

How would i write a query in DB2 SQL to only return the rows flagged with a 'Y'?

EDIT: The contiguous flag is something i have added manually to represent the records i would like to select, it does not exist on the table.

ID  From        To          Contiguous
ABC 01/01/2014  30/06/2014  Y
ABC 01/06/2013  01/01/2014  Y
ABC 01/05/2013  01/06/2013  Y
ABC 01/01/2013  01/02/2013  N
ABC 01/10/2012  01/01/2013  N

Thanks in advance! J

2

There are 2 answers

1
syllabus On

you will need a recursive select something like that:

WITH RECURSIVE
 contiguous_intervals(start, end) AS (
    select start, end
    from intervals
    where end = (select max(end) from intervals)
 UNION ALL
    select i.start, i.end
    from contiguous_intervals m, intervals i
    where i.end = m.start
)
select * from contiguous_intervals;
0
Gordon Linoff On

You can do this with lead(), lag(). I'm not sure what the exact logic is for your case, but I think it is something like:

select r.*,
       (case when (prev_to = from or prev_to is null) and
                  (next_from = to or next_from is null)
             then 'Y'
             else 'N'
        end) as Contiguous
from (select r.*, lead(from) over (partition by id order by from) as next_from,
             lag(to) over (partition by id order by to) as prev_to
      from readings r
     ) r;