oracle - SQL of group by in order by -


[raw data]  b    c  1  10   1  1  10   2 2  20   3 2  20   4 1  100  5 1  100  6  [wanted result]    sum_of_b 1    20 2    40 1    200 

it's unuseful query has simple 'group by' clause , 'dense_rank on partition by' because grouping works rows. want grouping in state of ordering. how write proper query?

you need identify groups of adjacent records. can using difference of row numbers approach -- assuming c orders rows. difference constant consecutive values of a same:

select a, sum(b) (select t.*,              (row_number() on (order c) -               row_number() on (partition order c)              ) grp       table t      ) t group grp, order min(c); 

Comments