CREATE TABLE sample_test_avik
(
PRODUCT_ID NUMBER,
START_DATE DATE,
END_DATE DATE,
PRICE_VALUE NUMBER
)
Table created.
INSERT INTO sample_test_avik values (1, to_date('01-JAN-2015','DD-MON-YYYY'),to_date('04-JAN-2015','DD-MON-YYYY'), 100)
1 row(s) inserted.
INSERT INTO sample_test_avik values (1, to_date('05-JAN-2015','DD-MON-YYYY'),to_date('11-JAN-2015','DD-MON-YYYY'), 120)
1 row(s) inserted.
INSERT INTO sample_test_avik values (1, to_date('12-JAN-2015','DD-MON-YYYY'),to_date('20-JAN-2015','DD-MON-YYYY'), 100)
1 row(s) inserted.
INSERT INTO sample_test_avik values (1, to_date('21-JAN-2015','DD-MON-YYYY'),to_date('27-JAN-2015','DD-MON-YYYY'), 100)
1 row(s) inserted.
INSERT INTO sample_test_avik values (1, to_date('28-JAN-2015','DD-MON-YYYY'),to_date('15-FEB-2015','DD-MON-YYYY'), 120)
1 row(s) inserted.
INSERT INTO sample_test_avik values (1, to_date('16-FEB-2015','DD-MON-YYYY'),to_date('04-MAR-2015','DD-MON-YYYY'), 150)
1 row(s) inserted.
INSERT INTO sample_test_avik values (1, to_date('05-MAR-2015','DD-MON-YYYY'),to_date('13-MAR-2015','DD-MON-YYYY'), 150)
1 row(s) inserted.
INSERT INTO sample_test_avik values (1, to_date('14-MAR-2015','DD-MON-YYYY'),to_date('30-APR-2015','DD-MON-YYYY'), 150)
1 row(s) inserted.
INSERT INTO sample_test_avik values (1, to_date('01-MAY-2015','DD-MON-YYYY'),to_date('10-MAY-2015','DD-MON-YYYY'), 150)
1 row(s) inserted.
INSERT INTO sample_test_avik values (1, to_date('11-MAY-2015','DD-MON-YYYY'),to_date('25-MAY-2015','DD-MON-YYYY'), 200)
1 row(s) inserted.
INSERT INTO sample_test_avik values (1, to_date('26-MAY-2015','DD-MON-YYYY'),to_date('31-MAY-2015','DD-MON-YYYY'), 240)
1 row(s) inserted.
commit
Statement processed.
select product_id, min(start_date), max(end_date) from (
select a.*,
row_number() over (partition by price_value order by start_date) rn,
row_number() over (order by start_date) rk,
row_number() over (order by start_date) -
row_number() over (partition by price_value order by start_date) grp
from sample_test_avik a
)
group by product_id, price_value, grp
order by 2
PRODUCT_ID | MIN(START_DATE) | MAX(END_DATE) | 1 | 01-JAN-15 | 04-JAN-15 | 1 | 05-JAN-15 | 11-JAN-15 | 1 | 12-JAN-15 | 27-JAN-15 | 1 | 28-JAN-15 | 15-FEB-15 | 1 | 16-FEB-15 | 10-MAY-15 | 1 | 11-MAY-15 | 25-MAY-15 | 1 | 26-MAY-15 | 31-MAY-15 |
---|
select * from sample_test_avik
match_recognize (
partition by product_id
order by start_date
measures first(a.start_date) as st,
nvl(b.end_date, a.end_date) as ed
pattern (a b*)
define
b as b.price_value = prev(b.price_value)
)
PRODUCT_ID | ST | ED | 1 | 01-JAN-15 | 04-JAN-15 | 1 | 05-JAN-15 | 11-JAN-15 | 1 | 12-JAN-15 | 27-JAN-15 | 1 | 28-JAN-15 | 15-FEB-15 | 1 | 16-FEB-15 | 10-MAY-15 | 1 | 11-MAY-15 | 25-MAY-15 | 1 | 26-MAY-15 | 31-MAY-15 |
---|