简体   繁体   中英

Select the first record of the last group when there are repeating groups

Trying to select the first record of the latest repeating STATUS groups for each POLICY_ID. How can I do this?

Edit/Note: There can be more than two status repetitions as shown in the last three rows.

View of the data:

在此处输入图片说明

Desired output:

在此处输入图片说明

SQL for data:

--drop table mytable;
create table mytable (ROW_ID Number(5), POLICY_ID Number(5), 
                      CHANGE_NO Number(5), STATUS VARCHAR(50), CHANGE_DATE DATE);

insert into mytable values (  81, 1, 1, 'A', date '2018-01-01');
insert into mytable values (  95, 1, 2, 'A', date '2018-01-02');
insert into mytable values ( 100, 1, 3, 'B', date '2018-01-03');
insert into mytable values ( 150, 1, 4, 'C', date '2018-01-04');
insert into mytable values ( 165, 1, 5, 'A', date '2018-01-05');
insert into mytable values ( 175, 1, 6, 'A', date '2018-01-06');
insert into mytable values ( 599, 2, 1, 'S', date '2018-01-11');
insert into mytable values ( 602, 2, 2, 'S', date '2018-01-12');
insert into mytable values ( 611, 2, 3, 'S', date '2018-01-13');
insert into mytable values ( 629, 2, 4, 'T', date '2018-01-14');
insert into mytable values ( 720, 2, 5, 'U', date '2018-01-15');
insert into mytable values ( 790, 2, 6, 'S', date '2018-01-16');
insert into mytable values ( 812, 2, 7, 'S', date '2018-01-17');
insert into mytable values ( 825, 2, 8, 'S', date '2018-01-18');

select * from mytable;

Hmmm . . .

select t.*
from (select t.*,
             row_number() over (partition by policy_id order by change_date asc) as seqnum
      from t
      where not exists (select 1
                        from t t2
                        where t2.policy_id = t.policy_id and
                              t2.status <> t.status and
                              t2.change_date > t.change_date
                       )
     ) t
where seqnum = 1;

The inner subquery finds all rows where -- for a given policy number -- there is no later row with a different status. That defines the last group of records.

It then uses row_number() to enumerate the rows. These outer query selects the first row for each policy_number .

You can use LEAD and LAG functions to identify the rows that begin a "repetition". The condition status <> previous status and status = next status will identify such rows.

SELECT *
FROM (
    SELECT cte1.*, ROW_NUMBER() OVER (PARTITION BY POLICY_ID ORDER BY CHANGE_DATE DESC) AS rn
    FROM (
            SELECT mytable.*, CASE WHEN
                STATUS <> LAG(STATUS, 1, '!') OVER (PARTITION BY POLICY_ID ORDER BY CHANGE_DATE) AND
                STATUS = LEAD(STATUS) OVER (PARTITION BY POLICY_ID ORDER BY CHANGE_DATE)
            THEN 1 END AS toselect
            FROM mytable
    ) cte1
    WHERE toselect = 1
) cte2
WHERE rn = 1

If you are using Oracle 12c you could use MATCH_RECOGNIZE :

SELECT ROW_ID, POLICY_ID, CHANGE_NO, STATUS, CHANGE_DATE
FROM mytable
MATCH_RECOGNIZE (
  PARTITION BY POLICY_ID
  ORDER BY CHANGE_DATE
  MEASURES MATCH_NUMBER() m,FIRST(R.ROW_ID) r
  ALL ROWS PER MATCH
  PATTERN (R+)
  DEFINE R AS STATUS=NEXT(STATUS)
) MR
WHERE ROW_ID = R
ORDER BY ROW_NUMBER() OVER(PARTITION BY POLICY_ID ORDER BY M DESC)
FETCH FIRST 1 ROW WITH TIES;

db<>fiddle demo


Alternatively:

SELECT *
FROM mytable 
MATCH_RECOGNIZE (
  PARTITION BY POLICY_ID
  ORDER BY CHANGE_DATE DESC
  MEASURES MATCH_NUMBER() m
           ,LAST(R.ROW_ID) ROW_ID
           ,LAST(R.STATUS) STATUS
           ,LAST(R.CHANGE_NO) CHANGE_NO
           ,LAST(R.CHANGE_DATE) CHANGE_DATE
  ONE ROW PER MATCH
  PATTERN (R+)
  DEFINE R AS STATUS=PREV(STATUS)
) MR
WHERE M = 1

db<>fiddle demo2

Another approach with match_recognize :

select row_id, policy_id, change_no, status, change_date
from   mytable
       match_recognize (
           partition by policy_id
           order by change_date
           measures
             strt.row_id as row_id
           , strt.change_no as change_no
           , strt.change_date as change_date
           , strt.status as status
           pattern (strt unchanged* final)
           define
             unchanged as next(unchanged.status) = prev(unchanged.status)
           , final as next(final.status) is null
      ) mr
order by mr.policy_id;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM