sql >> Database teknologi >  >> RDS >> Oracle

Oracle SQL rækkesammenkædning efter punktum

Dette er en form for huller-og-øer-problem. Du kan bruge lag() og derefter en kumulativ sum:

select id, min(laufd), max(nextdt),
       row_number() over (partition by id order by min(laufd)) as period
from (select t.*,
             sum(case when prev_nextdt >= laufd - interval '2' day then 0 else 1 end) over
                 (partition by id order by order_row) as grp
      from (select t.*,
                   lag(nextdt) over (partition by id order by order_row) as prev_nextdt
            from t
           ) t
     ) t
group by grp, id;

EDIT:

Hvis værdierne er gemt som strenge, skal du bruge:

select id, min(laufd), max(nextdt),
       row_number() over (partition by id order by min(laufd)) as period
from (select t.*,
             sum(case when prev_nextdt >= laufd - interval '2' day then 0 else 1 end) over
                 (partition by id order by order_row) as grp
      from (select t.id, t.order_row, -- any other columns you need
                   to_date(laufd, 'YYYYMMDD') as laufd,
                   to_date(nextdt, 'YYYYMMDD') as next_dt,
                   lag(to_date(nextdt, 'YYYYMMDD')) over (partition by id order by order_row) as prev_nextdt
            from t
           ) t
     ) t
group by grp, id;


  1. ORA-00932:inkonsistente datatyper:forventet - fik -

  2. Cygnus-NGSI gemmer ikke data i PostgreSQL

  3. Hvordan skelner man mellem de samme feltnavne på to tabeller i en udvalgt forespørgsel?

  4. BESTIL VED en tilsvarende værdi i MySQL