Oracle SQL: Detecting breaks in continual spans -
i have following table , i'm trying detect products have break in spans.
product | unit_cost | price start date | price end date -------------------------------------------------------------------------- product 1 15.00 01/01/2011 03/31/2011 product 1 15.00 04/01/2011 06/31/2011 product 1 15.00 07/01/2011 09/31/2011 product 1 15.00 10/01/2011 12/31/2011 product 2 10.00 01/01/2011 12/31/2011 product 3 25.00 01/01/2011 06/31/2011 product 3 25.00 10/01/2011 12/31/2011
so here want report product3 because missing span
07/01/2011 - 09/31/2011
any ideas on how can this?
edit: oracle ver: 10g
create table statement create table sandbox.tbl_product ( product_id varchar2(13 byte), product varchar2(64 byte), unit_cost number, price_start_date date, price_end_date date )
edit 2 start dates , end dates cannot overlap
edit 3 span can 2 dates long price_end_date >= price_start_date. equal included since product can on sale 1 day.
try (using lead analytic function):
select * ( select a.*, lead(price_start_date,1,null) over(partition product order price_end_date) next_start_date product ) (price_end_date + 1)<> next_start_date
example setup
create table product ( product varchar2(100 byte), unit_cost number, start_date date, end_date date ); insert product values('product 1','15.00',to_date('01/01/2011','mm/dd/rrrr'),to_date('03/31/2011','mm/dd/rrrr')); insert product values('product 1','15.00',to_date('04/01/2011','mm/dd/rrrr'),to_date('06/30/2011','mm/dd/rrrr')); insert product values('product 1','15.00',to_date('07/01/2011','mm/dd/rrrr'),to_date('09/30/2011','mm/dd/rrrr')); insert product values('product 1','15.00',to_date('10/01/2011','mm/dd/rrrr'),to_date('12/31/2011','mm/dd/rrrr')); insert product values('product 2','10.00',to_date('01/01/2011','mm/dd/rrrr'),to_date('12/31/2011','mm/dd/rrrr')); insert product values('product 3','25.00',to_date('01/01/2011','mm/dd/rrrr'),to_date('06/30/2011','mm/dd/rrrr')); insert product values('product 3','25.00',to_date('10/01/2011','mm/dd/rrrr'),to_date('12/31/2011','mm/dd/rrrr')); select * ( select a.*, lead(start_date,1,null) over(partition product order start_date) next_start_date product ) (end_date + 1)<> next_start_date
edit:updated query consider next start_date , current end_date avoid issues distribution of data.
Comments
Post a Comment