, Oracle 10 . model ( match_recognize). match_recognize, Oracle 12 , , , OP ( oracle11g).
alter session set nls_date_format = 'dd/mm/yyyy';
create table test_data ( telephone, motive, complaint_id, complaint_date ) as
select 980761524, 'motive1', 'R1234561', to_date('23/05/2017') from dual union all
select 980761524, 'motive1', 'R1234562', to_date('23/05/2017') from dual union all
select 980761524, 'motive1', 'R1234563', to_date('25/08/2017') from dual union all
select 980761524, 'motive1', 'R1234564', to_date('26/09/2017') from dual union all
select 980761524, 'motive1', 'R1234565', to_date('10/10/2017') from dual union all
select 980761524, 'motive1', 'R1234566', to_date('30/12/2017') from dual union all
select 991761525, 'motive2', 'R4454222', to_date('24/06/2017') from dual union all
select 991761525, 'motive2', 'R4454223', to_date('29/06/2017') from dual union all
select 991761525, 'motive2', 'R4454224', to_date('30/10/2017') from dual union all
select 940789563, 'motive3', 'R8993271', to_date('24/06/2017') from dual union all
select 940789563, 'motive3', 'R8993272', to_date('29/06/2017') from dual union all
select 940789563, 'motive3', 'R8993273', to_date('30/10/2017') from dual
;
commit;
Query
select telephone, motive, complaint_id, complaint_date, flag
from test_data
model
partition by (telephone, motive)
dimension by (row_number() over (partition by telephone, motive
order by complaint_date, complaint_id) rn)
measures (complaint_id, complaint_date, complaint_date s, 0 flag)
rules (
s[rn>1] = case when complaint_date[cv(rn)] < add_months(s[cv(rn) - 1], 2)
then s[cv(rn) - 1]
else complaint_date[cv(rn)]
end,
flag[rn>1] = case when s[cv(rn)] = s[cv(rn) - 1] then 1 else 0 end
)
order by telephone, motive, rn
;
: ( , - )
TELEPHONE MOTIVE COMPLAINT_ID COMPLAINT_DATE FLAG
--------- ------- ------------ -------------- ----
940789563 motive3 R8993271 24/06/2017 0
940789563 motive3 R8993272 29/06/2017 1
940789563 motive3 R8993273 30/10/2017 0
980761524 motive1 R1234561 23/05/2017 0
980761524 motive1 R1234562 23/05/2017 1
980761524 motive1 R1234563 25/08/2017 0
980761524 motive1 R1234564 26/09/2017 1
980761524 motive1 R1234565 10/10/2017 1
980761524 motive1 R1234566 30/12/2017 0
991761525 motive2 R4454222 24/06/2017 0
991761525 motive2 R4454223 29/06/2017 1
991761525 motive2 R4454224 30/10/2017 0