正好刚刚在另一个问题里写了个类似的,改了一下
with t(pwlock,status,teller_no,num_prefix) as (
select 2,1,222222,'YY' from dual union all
select 3,1,222222,'YY' from dual union all
select 4,1,222222,'YY' from dual union all
select 5,0,222222,'YY' from dual union all
select 5,0,222222,'WW' from dual union all
select 6,0,222222,'WW' from dual union all
select 7,0,222222,'WW' from dual union all
select 8,0,222222,'YY' from dual union all
select 8,0,222222,'WW' from dual union all
select 9,0,222222,'YY' from dual union all
select 10,0,222222,'YY' from dual union all
select 1,0,333333,'WW' from dual union all
select 2,0,333333,'WW' from dual union all
select 3,0,333333,'WW' from dual union all
select 4,0,333333,'WW' from dual union all
select 9,0,333333,'WW' from dual union all
select 10,0,333333,'WW' from dual union all
select 1,1,444444,'YY' from dual union all
select 6,0,444444,'YY' from dual union all
select 7,0,444444,'YY' from dual )
,T2 AS (
select T.*,ROW_NUMBER() OVER(PARTITION BY teller_no,num_prefix ORDER BY PWLOCK) RN from T
),
T3(pwlock,status,teller_no,num_prefix) AS (
SELECT pwlock,status,teller_no,num_prefix FROM T2 WHERE T2.RN=1
UNION ALL
SELECT T3.pwlock+1,NVL(T2.status,1),T3.teller_no,T3.num_prefix FROM T3 LEFT JOIN T2 ON
T3.teller_no=T2.teller_no AND T3.num_prefix=T2.num_prefix AND T3.PWLOCK=T2.PWLOCK-1
WHERE T3.PWLOCK<=10
),
T4 AS (
select PWLOCK,STATUS,TELLER_NO,NUM_PREFIX,cnt,
LAG(PWLOCK,CASE WHEN CNT=0 THEN 0 ELSE CNT-1 END ) OVER(PARTITION BY TELLER_NO,NUM_PREFIX ORDER BY PWLOCK) L from t3
match_recognize(
partition by TELLER_NO,NUM_PREFIX
order by PWLOCK
measures count(decode(STATUS,1,null,1)) cnt
ALL ROWS PER MATCH
pattern(a* b)
define a as STATUS = next(STATUS)
))
select MIN(PWLOCK),MAX(PWLOCK),TELLER_NO, NUM_PREFIX from T4 WHERE CNT>0 GROUP BY L,TELLER_NO, NUM_PREFIX