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

Query Oracle SQL per raggruppare record consecutivi

Prova questa query:

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