create table prodT(prod,day1,Val) as
select 'ABC',to_date('6/12/2009','fmmm/dd/yyyy'),1 from dual union all
select 'ABC',to_date('6/13/2009','fmmm/dd/yyyy'),1 from dual union all
select 'ABC',to_date('6/14/2009','fmmm/dd/yyyy'),1 from dual union all
select 'ABC',to_date('6/15/2009','fmmm/dd/yyyy'),0 from dual union all
select 'ABC',to_date('6/16/2009','fmmm/dd/yyyy'),0 from dual union all
select 'ABC',to_date('6/17/2009','fmmm/dd/yyyy'),1 from dual union all
select 'ABC',to_date('6/18/2009','fmmm/dd/yyyy'),1 from dual union all
select 'ABC',to_date('6/19/2009','fmmm/dd/yyyy'),1 from dual union all
select 'ABC',to_date('6/20/2009','fmmm/dd/yyyy'),1 from dual union all
select 'ABC',to_date('6/21/2009','fmmm/dd/yyyy'),1 from dual union all
select 'ABC',to_date('6/22/2009','fmmm/dd/yyyy'),1 from dual union all
select 'DEF',to_date('6/18/2009','fmmm/dd/yyyy'),1 from dual union all
select 'DEF',to_date('6/19/2009','fmmm/dd/yyyy'),1 from dual union all
select 'DEF',to_date('6/20/2009','fmmm/dd/yyyy'),0 from dual union all
select 'DEF',to_date('6/21/2009','fmmm/dd/yyyy'),0 from dual union all
select 'DEF',to_date('6/22/2009','fmmm/dd/yyyy'),0 from dual;
--■■■旅人算の感覚を使う方法1■■■
select prod,min(day1),count(*)
from (select prod,day1,Val,
Row_Number() over(partition by prod order by day1)
-Row_Number() over(partition by prod,Val order by day1)
as distance
from prodT)
where Val > 0
group by prod,distance
order by min(day1);
--■■■旅人算の感覚を使う方法2■■■
select prod,min(day1),count(*)
from (select prod,day1,Val,
day1-Row_Number() over(partition by prod order by day1)
as distance
from prodT
where Val > 0)
group by prod,distance
order by min(day1);