Oracle-løsning:
with l as ( select level lvl from dual connect by level < 66 ),
r as ( select range_start r1, range_end r2, range_val v,
range_end - range_start + 1 cnt
from ranges ),
t1 as (select distinct lvl,
nvl(max(v) keep (dense_rank first order by cnt)
over (partition by lvl), '*' ) m
from l left join r on lvl between r1 and r2 ),
t2 as (select lvl, m, case when lag(m) over (order by lvl) <> m then 0 else 1 end mrk
from t1),
t3 as (select lvl, m, lvl - sum(mrk) over (order by lvl) grp from t2)
select min(lvl) r1, max(lvl) r2, nullif(min(m), '*') val
from t3 group by grp order by r1
Output er som ønsket. Mit engelsk er langt fra godt, så det er svært at forklare, men lad os prøve:
l
- talgenerator,r
- data fraranges
med optalt afstand,t1
- finder værdi med minimal afstand for hver lvl,t2
- tilføjer markører, der fortæller om rækkevidde starter,t3
- tilføjer kolonne, som vi næste gang vil bruge til gruppering af data.