Oracle
 sql >> Base de Dados >  >> RDS >> Oracle

Consulta Oracle SQL para agrupar registros consecutivos


Tente esta consulta:
SELECT line_no,
       amount,
       narration,
       SUM( x ) OVER ( ORDER BY line_no
                       ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW 
       ) as calc_group
FROM (
  SELECT t.*,
         CASE lag( narration ) OVER (order by line_no ) 
         WHEN narration THEN 0
         ELSE 1 END x
  FROM test t

)
ORDER BY line_no

demo --> http://www.sqlfiddle.com/#!4/6d7aa /9