Oracle
 sql >> Datenbank >  >> RDS >> Oracle

Zeilen gruppieren, wobei die Reihenfolge der Werte beibehalten wird

Aktualisiert: Antwort gemäß Kommentar korrigiert.

Zeilen können nach Bedarf so gruppiert werden:

-- leave only first rows of each group and substitute col2 with a sum.
select 
  dateno, 
  col1, 
  group_sum as col2 
from (
  -- Get sum of col2 for each bucket 
  select 
    dateno, 
    col1, 
    is_start, 
    sum(col2) over (partition by bucket_number) group_sum
  from (
    -- divide rows into buckets based on previous col1 change count
    select
      dateno, col1, col2, is_start, 
      sum(is_start) over(order by dateno rows unbounded preceding) bucket_number
    from (
      -- mark rows with change of col1 value as start of new sequence 
      select
        dateno, col1, col2,
        decode (nvl(prev_col1, col1||'X'), col1, 0, 1) is_start
      from (
        -- determine for each row value of col1 in previous row. 
        select 
          dateno, 
          col1, 
          col2,
          lag(col1) over (order by dateno)  prev_col1
        from t 
      )  
    )  
  )
)
where is_start = 1
order by dateno

Beispiel bei SQLFiddle