1
select * from grouping;

上面的查询给出:

WORKING_DAYS,DATE_TYPE,SEQ_START
10/11/2013  ,L        ,0
10/10/2013  ,L        ,0
10/8/2013   ,L        ,1
10/6/2013   ,H        ,0
10/5/2013   ,H        ,0
10/4/2013   ,L        ,0
10/3/2013   ,L        ,0
10/2/2013   ,L        ,0
10/1/2013   ,L        ,0

我想按 SEQ_START 对这些行进行分组,以便在运行时它应该给出以下结果。rank 列应该是按 SEQ_START 分组的 WORKING_DAYS(或任何其他数字)。请注意,即使 10/10/2013 和 10/11/2013 的 SEQ_START 为 0,但它仍然不应该有 10/1/2013 而应该有 10/10/2013,因为在 2013 年 10 月 8 日,序列是损坏(SEQ_START 为 0)。

WORKING_DAYS,DATE_TYPE,SEQ_START,rank
10/1/2013   ,L        ,0        ,10/1/2013
10/2/2013   ,L        ,0        ,10/1/2013
10/3/2013   ,L        ,0        ,10/1/2013
10/4/2013   ,L        ,0        ,10/1/2013
10/8/2013   ,L        ,1        ,10/8/2013
10/10/2013  ,L        ,0        ,10/10/2013
10/11/2013  ,L        ,0        ,10/10/2013

我已经写了以下查询,它的返回几乎是我所期望的,但它给 10/10/2013 和 10/11/2013 一个“0”值,而不是它们应该有 10/10/2013,因为序列在 10/8/ 2013,其中 1 代表 SEQ_START

select working_days,  
       date_type, 
       seq_start, 
       FIRST_VALUE(working_days)  OVER (PARTITION BY seq_start 
                                        ORDER BY working_days) "rank"
  from grouping 
 where date_type = 'L'
4

1 回答 1

1
select 
  working_days,  date_type, seq_start, 
  FIRST_VALUE(working_days) OVER (PARTITION BY grp ORDER BY working_days) "rank"
from (
  select 
    working_days, date_type, seq_start,
    sum(front) over(order by working_days) as grp
  from (
    select 
      working_days, date_type, seq_start,
      decode(seq_start, lag(seq_start)over(order by working_days), 0, 1) as front
    from t1
  )
)
where date_type = 'L'
order by 1 

小提琴

于 2013-10-01T17:03:25.190 回答