create table running_log (
run_date date not null,
time_in_seconds int not null,
distance_in_miles int not null
)
Table created.
begin
insert into running_log values (date'2018-01-01', 420, 1);
insert into running_log values (date'2018-01-02', 2400, 5);
insert into running_log values (date'2018-01-03', 2430, 5);
insert into running_log values (date'2018-01-06', 2350, 5);
insert into running_log values (date'2018-01-07', 410, 1);
insert into running_log values (date'2018-01-10', 400, 1);
insert into running_log values (date'2018-01-13', 2300, 5);
insert into running_log values (date'2018-01-14', 425, 1);
insert into running_log values (date'2018-01-15', 422, 1);
commit;
end;
1 row(s) inserted.
select * from running_log
RUN_DATE | TIME_IN_SECONDS | DISTANCE_IN_MILES | 01-JAN-18 | 420 | 1 | 02-JAN-18 | 2400 | 5 | 03-JAN-18 | 2430 | 5 | 06-JAN-18 | 2350 | 5 | 07-JAN-18 | 410 | 1 | 10-JAN-18 | 400 | 1 | 13-JAN-18 | 2300 | 5 | 14-JAN-18 | 425 | 1 | 15-JAN-18 | 422 | 1 |
---|
Tabibitosan method
select run_date, row_number() over (order by run_date) rn ,
run_date - row_number() over (order by run_date) grp_date
from running_log
RUN_DATE | RN | GRP_DATE | 01-JAN-18 | 1 | 31-DEC-17 | 02-JAN-18 | 2 | 31-DEC-17 | 03-JAN-18 | 3 | 31-DEC-17 | 06-JAN-18 | 4 | 02-JAN-18 | 07-JAN-18 | 5 | 02-JAN-18 | 10-JAN-18 | 6 | 04-JAN-18 | 13-JAN-18 | 7 | 06-JAN-18 | 14-JAN-18 | 8 | 06-JAN-18 | 15-JAN-18 | 9 | 06-JAN-18 |
---|
Tabibitosan method summary
with grps as (
select run_date, row_number() over (order by run_date) rn ,
run_date - row_number() over (order by run_date) grp_date
from running_log
)
select min(run_date) first_run, max(run_date) last_run,
count(*) runs,
row_number() over (order by min(run_date)) grp
from grps
group by grp_date
order by min(run_date)
FIRST_RUN | LAST_RUN | RUNS | GRP | 01-JAN-18 | 03-JAN-18 | 3 | 1 | 06-JAN-18 | 07-JAN-18 | 2 | 2 | 10-JAN-18 | 10-JAN-18 | 1 | 3 | 13-JAN-18 | 15-JAN-18 | 3 | 4 |
---|
Pattern matching method
select *
from running_log
match_recognize (
order by run_date
measures
classifier() as var,
match_number() as grp
all rows per match
pattern ( strt consecutive* )
define
consecutive as run_date = ( prev ( run_date ) + 1 )
)
RUN_DATE | VAR | GRP | TIME_IN_SECONDS | DISTANCE_IN_MILES | 01-JAN-18 | STRT | 1 | 420 | 1 | 02-JAN-18 | CONSECUTIVE | 1 | 2400 | 5 | 03-JAN-18 | CONSECUTIVE | 1 | 2430 | 5 | 06-JAN-18 | STRT | 2 | 2350 | 5 | 07-JAN-18 | CONSECUTIVE | 2 | 410 | 1 | 10-JAN-18 | STRT | 3 | 400 | 1 | 13-JAN-18 | STRT | 4 | 2300 | 5 | 14-JAN-18 | CONSECUTIVE | 4 | 425 | 1 | 15-JAN-18 | CONSECUTIVE | 4 | 422 | 1 |
---|
Pattern matching summary results
select *
from running_log
match_recognize (
order by run_date
measures
first(run_date) as first_run,
last(run_date) as last_run,
count(*) as runs,
match_number() as grp
pattern ( strt consecutive* )
define
consecutive as run_date = ( prev ( run_date ) + 1 )
)
FIRST_RUN | LAST_RUN | RUNS | GRP | 01-JAN-18 | 03-JAN-18 | 3 | 1 | 06-JAN-18 | 07-JAN-18 | 2 | 2 | 10-JAN-18 | 10-JAN-18 | 1 | 3 | 13-JAN-18 | 15-JAN-18 | 3 | 4 |
---|