0

如何对以下数据集进行分组:

DATENO    COL1   COL2
  1         A      1     
  2         B      1
  3         C      1
  4         C      1
  5         D      1
  6         C      1
  7         D      1
  8         D      1
  9         E      1

要得到这样的东西:

DATENO    COL1   COL2
  1        A      1     
  2        B      1
  3        C      2
  5        D      1
  6        C      1
  7        D      2
  9        E      1

C 和 D 的总和被分组,保持顺序不变。有任何想法吗?

4

1 回答 1

0

更新:根据评论更正答案。

可以按以下方式对行进行分组:

-- 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

SQLFiddle的示例

于 2013-06-07T16:12:45.703 回答