sql >> Base de Datos >  >> RDS >> Oracle

Oracle SQL cómo agrupar, pero tener varias filas si el grupo se repite en una fecha posterior

Este es un problema de lagunas e islas. Hay diferentes enfoques, pero uno simple usa una diferencia de número de fila:

with paaf as (<your first query here>
     )
select paaf.assignment_id,
       paaf.position_id,
       min(paaf.effective_start_date) as effective_start_date,
       max(paaf.effective_end_date) as effective_end_date
from (select paaf.*,
             row_number() over (order by effective_start_date) as seqnum,
             row_number() over (partition by position_id order by effective_start_date) as seqnum_p
      from paaf
     ) paaf
group by (seqnum - seqnum_p), position_id, assignment_id;