CUST_KEY NUMBER
DATE_IN DATE
DATE_OUT DATE
대략 이런 구조 입니다.
여기서 7일 연속 접속하는 고객 데이터를 알고 싶습니다.
#in - out범위가 24시간을 벗어나지 않는다는 가정입니다.
with dat(cust_key,date_in,date_out) as( select 1,'20150101','20150101'from dual union all select 1,'20150103','20150103'from dual union all select 1,'20150104','20150104'from dual union all select 1,'20150105','20150105'from dual union all select 1,'20150106','20150106'from dual union all select 1,'20150107','20150108'from dual union all select 1,'20150109','20150109'from dual union all select 2,'20150101','20150101'from dual ) ,t as(select cust_key,to_date(date_in,'yyyymmdd') date_in,to_date(date_out,'yyyymmdd') date_out from dat) select cust_key,min(date_in) date_in, max(real) date_out from( select t.*, sum(bit) over(order by cust_key, date_in) pic from( select t.* , lead(date_in, 6) over(partition by cust_key order by date_in) real , sign(lag(date_in) over(partition by cust_key order by date_in) - ( date_in - 1)) bit from (select cust_key, date_in from t union select cust_key, date_out from t) t ) t where date_in + 6 = real ) t group by cust_key, pic order by cust_key,date_in
#벗어날 수 있다면
select t.cust_key, min(t.date_in) date_in, max(t.date_out) date_out,max(csum) days from( select t.*,sum(bit) over(partition by t.cust_key, pic) csum from( select * from t model dimension by( row_number() over(order by rownum) rn) measures(cust_key, date_in, date_out, date_out - date_in + 1 as bit, 0 pic) rules(pic[any] = case when date_out[cv()-1] + 1 = date_in[cv()] then nvl(pic[cv()-1],0) else nvl(pic[cv()-1],0) + 1 end ) ) t ) t where csum >= 7 group by t.cust_key, t.pic order by t.cust_key, t.pic
김홍선 님의 빈구간 구하기를 응용하면 in-out 범위 제한 없이도 원하는 결과가 가능합니다.
database.sarang.net/index.php