oracle11g - Interval of consecutive numbers with Oracle SQL and JOINS -
i'm trying display intervals of consecutive elevations in iceland, per row, in form this:
elevation 0 27 29 33 35 37 40 42 46 48 51 63 for now, managed track gaps, have in mind like:
if (count of values in columnb until columnc='gap') equals value of columnb columnc='gap' have consecutive interval between value of columna , column b
can give me tips ?
current code
with x ( select distinct elevation cities iso = 'is' , iso not null ),y ( select a.elevation "a",b.elevation "b",c.elevation "c" x join x b on b.elevation > a.elevation left join x c on c.elevation > b.elevation , c.elevation < b.elevation + 2 )select y.a,y.b,y.c,case when y.c null 'gap' else ' ' end gapz y order 1,2 output
b c gap ------------------ 0 1 2 0 2 3 0 3 4 ... ... ... 0 25 26 0 26 27 0 27 gap 0 29 30 0 30 31 0 31 32 0 32 33 0 33 gap 0 35 gap 0 37 38 0 38 39 0 39 40 0 40 gap 0 42 43 0 43 44 0 44 45 0 45 46 0 46 gap 0 48 gap 0 51 52 0 52 53 ... ... ... 0 61 62 0 62 63 0 63 gap 0 65 66 0 66 67 0 67 68 0 68 69 0 69 gap 0 71 72 ... ... ...
you're halfway there! need flag either end of gap, select rows.
select x, case lead(x,x) over(order x) when x+1 null else x end endpoint, case lag(x,x) over(order x) when x-1 null else x end startpoint table this show whether row start or endpoint. let's call q1. select need that.
select q1.startpoint, (select min(endpoint) q1 endp endp.endpoint >= q1.x) endpoint q1 q1.startpoint not null
Comments
Post a Comment