insert into candy
select level
,regexp_substr(s,'[^,]+',1,level)
from (select '1,3,2,2,2,1,3,4,4,4,4,6,6,6,5,4,3,3,3,7,8,9,9,9,3,2,5,5,5' s from dual
)
connect by level<=regexp_count(s,',')+1;
如果数字连续,每人糖块和原始分数一样
with recursive t(id,rt,candy)
as(select id,rating-min(rating)over(),1,from candy
union all
select id,rt-min(rt)over(),candy+1 from t where rt>0
)
select id,max(candy) from t group by id order by id;
select id,max(rating) as rating,max(candy_cnt) as candy_count --------- 起终点在结果中都会出现两次,属于两个不同的段,取其最大值
from candy
match_recognize (
order by id
measures
match_number() as grp
,CLASSIFIER() AS var_match
,count(*) as rn
,final count(*) as cnt
,case
WHEN CLASSIFIER()='END_POINT' and rating>prev(rating) --------- 上升段
or CLASSIFIER()='UP' then count(*)
when CLASSIFIER()='END_POINT' and rating<prev(rating) --------- 下降段
or CLASSIFIER()='DOWN' then final count(*) - RUNNING count(*)+1
ELSE 1 ------- 平直段
end as candy_cnt
all rows per match
after match skip to last end_point --------- 每一段的终点都是下一段的起点
pattern( up+ end_point | flat+ end_point | down+ end_point)
define up as rating<next(rating)
,flat as rating=next(rating)
,down as rating>next(rating)
)
group by id --------- 起终点在结果中都会出现两次,属于两个不同的段
order by id;