create table records
( seq number(5)
, payload varchar2(200)
)
Table created.
insert into records values (1, 'red')
1 row(s) inserted.
insert into records values (2, 'blue')
1 row(s) inserted.
insert into records values (3, 'yellow')
1 row(s) inserted.
insert into records values (4, 'red')
1 row(s) inserted.
insert into records values (5, 'white')
1 row(s) inserted.
insert into records values (6, 'blue')
1 row(s) inserted.
insert into records values (7, 'red')
1 row(s) inserted.
insert into records values (8, 'yellow')
1 row(s) inserted.
insert into records values (9, 'blue')
1 row(s) inserted.
insert into records values (29, 'red')
1 row(s) inserted.
insert into records values (11, 'white')
1 row(s) inserted.
insert into records values (12, 'red')
1 row(s) inserted.
insert into records values (13, 'blue')
1 row(s) inserted.
insert into records values (14, 'blue')
1 row(s) inserted.
insert into records values (15, 'green')
1 row(s) inserted.
insert into records values (16, 'blue')
1 row(s) inserted.
insert into records values (17, 'yellow')
1 row(s) inserted.
insert into records values (18, 'red')
1 row(s) inserted.
insert into records values (19, 'yellow')
1 row(s) inserted.
insert into records values (20, 'red')
1 row(s) inserted.
insert into records values (10, 'blue')
1 row(s) inserted.
insert into records values (119, 'white')
1 row(s) inserted.
insert into records values (120, 'blue')
1 row(s) inserted.
insert into records values (121, 'red')
1 row(s) inserted.
The observed color sequences
with sequences as
( select seq
, payload
||'|'||lead(payload) over (order by seq)
||'|'||lead(payload,2) over (order by seq) color_sequence
, row_number() over (order by seq desc) rows_to_come
from records
)
select distinct color_sequence
from sequences
where rows_to_come > 2
COLOR_SEQUENCE | red|yellow|red | red|blue|yellow | blue|blue|green | blue|white|red | yellow|red|white | red|red|white | red|yellow|blue | red|white|blue | blue|green|blue | blue|blue|white | yellow|blue|blue | yellow|red|yellow | blue|yellow|red | green|blue|yellow | blue|red|yellow | red|blue|blue | white|red|blue | white|blue|red | yellow|red|red |
---|
Count occurrences per color sequence
with sequences as
( select seq
, payload
||'|'||lead(payload) over (order by seq)
||'|'||lead(payload,2) over (order by seq) color_sequence
, row_number() over (order by seq desc) rows_to_come
from records
)
select color_sequence
, count(seq) occurrence_count
from sequences
where rows_to_come > 2
group
by color_sequence
order
by occurrence_count desc
COLOR_SEQUENCE | OCCURRENCE_COUNT | white|blue|red | 2 | blue|yellow|red | 2 | red|white|blue | 2 | blue|white|red | 1 | yellow|red|white | 1 | red|red|white | 1 | red|yellow|blue | 1 | blue|green|blue | 1 | blue|blue|white | 1 | yellow|blue|blue | 1 | yellow|red|yellow | 1 | green|blue|yellow | 1 | blue|red|yellow | 1 | red|blue|blue | 1 | white|red|blue | 1 | blue|blue|green | 1 | yellow|red|red | 1 | red|yellow|red | 1 | red|blue|yellow | 1 |
---|
Find top 3 occurring sequences (pre 12c approach)
with sequences as
( select seq
, payload
||'|'||lead(payload) over (order by seq)
||'|'||lead(payload,2) over (order by seq) color_sequence
, row_number() over (order by seq desc) rows_to_come
from records
)
select *
from ( select color_sequence
, count(seq) occurrence_count
from sequences
where rows_to_come > 2
group
by color_sequence
order
by occurrence_count desc
)
where rownum < 4
COLOR_SEQUENCE | OCCURRENCE_COUNT | red|white|blue | 2 | white|blue|red | 2 | blue|yellow|red | 2 |
---|
All possible combinations of three colors
with colors as
(select distinct payload color
from records
)
select c1.color||'|'||c2.color||'|'||c3.color
from colors c1
cross join
colors c2
cross join
colors c3
C1.COLOR||'|'||C2.COLOR||'|'||C3.COLOR | green|green|green | green|green|red | green|green|blue | green|green|white | green|green|yellow | green|red|green | green|red|red | green|red|blue | green|red|white | green|red|yellow | green|blue|green | green|blue|red | green|blue|blue | green|blue|white | green|blue|yellow | green|white|green | green|white|red | green|white|blue | green|white|white | green|white|yellow | green|yellow|green | green|yellow|red | green|yellow|blue | green|yellow|white | green|yellow|yellow | red|green|green | red|green|red | red|green|blue | red|green|white | red|green|yellow | red|red|green | red|red|red | red|red|blue | red|red|white | red|red|yellow | red|blue|green | red|blue|red | red|blue|blue | red|blue|white | red|blue|yellow | red|white|green | red|white|red | red|white|blue | red|white|white | red|white|yellow | red|yellow|green | red|yellow|red | red|yellow|blue | red|yellow|white | red|yellow|yellow | blue|green|green | blue|green|red | blue|green|blue | blue|green|white | blue|green|yellow | blue|red|green | blue|red|red | blue|red|blue | blue|red|white | blue|red|yellow | blue|blue|green | blue|blue|red | blue|blue|blue | blue|blue|white | blue|blue|yellow | blue|white|green | blue|white|red | blue|white|blue | blue|white|white | blue|white|yellow | blue|yellow|green | blue|yellow|red | blue|yellow|blue | blue|yellow|white | blue|yellow|yellow | white|green|green | white|green|red | white|green|blue | white|green|white | white|green|yellow | white|red|green | white|red|red | white|red|blue | white|red|white | white|red|yellow | white|blue|green | white|blue|red | white|blue|blue | white|blue|white | white|blue|yellow | white|white|green | white|white|red | white|white|blue | white|white|white | white|white|yellow | white|yellow|green | white|yellow|red | white|yellow|blue | white|yellow|white | white|yellow|yellow | yellow|green|green | yellow|green|red | yellow|green|blue | yellow|green|white | yellow|green|yellow | yellow|red|green | yellow|red|red | yellow|red|blue | yellow|red|white | yellow|red|yellow | yellow|blue|green | yellow|blue|red | yellow|blue|blue | yellow|blue|white | yellow|blue|yellow | yellow|white|green | yellow|white|red | yellow|white|blue | yellow|white|white | yellow|white|yellow | yellow|yellow|green | yellow|yellow|red | yellow|yellow|blue | yellow|yellow|white | yellow|yellow|yellow |
---|
Finding possible sequences that never actually occurred
with colors as
(select distinct payload color
from records
)
, combinations as
( select c1.color||'|'||c2.color||'|'||c3.color combination
from colors c1
cross join
colors c2
cross join
colors c3
)
, sequences as
( select seq
, payload
||'|'||lead(payload) over (order by seq)
||'|'||lead(payload,2) over (order by seq) color_sequence
, row_number() over (order by seq desc) rows_to_come
from records
)
select c.combination
from combinations c
left outer join
sequences s
on (c.combination = s.color_sequence)
where s.rowid is null