database.sarang.net
UserID
Passwd
Database
DBMS
MySQL
PostgreSQL
Firebird
ㆍOracle
Informix
Sybase
MS-SQL
DB2
Cache
CUBRID
LDAP
ALTIBASE
Tibero
DB 문서들
스터디
Community
공지사항
자유게시판
구인|구직
DSN 갤러리
도움주신분들
Admin
운영게시판
최근게시물
Oracle Q&A 40704 게시물 읽기
No. 40704
7일 연속 로그인한 고객데이터 뽑는 쿼리 부탁 드립니다.
작성자
사바직자
작성일
2015-02-02 14:12
조회수
8,106

 CUST_KEY   NUMBER

DATE_IN       DATE

DATE_OUT       DATE

 

대략 이런 구조 입니다.

 

여기서 7일 연속 접속하는 고객 데이터를 알고 싶습니다.

이 글에 대한 댓글이 총 2건 있습니다.

#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

 

최성준(junkk)님이 2015-02-02 16:40에 작성한 댓글입니다.
이 댓글은 2015-02-04 16:55에 마지막으로 수정되었습니다.

 김홍선 님의 빈구간 구하기를 응용하면 in-out 범위 제한 없이도 원하는 결과가 가능합니다.

database.sarang.net/index.php

 

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 union all
select 2,'20150103','20150108'from dual union all
select 3,'20150101','20150109'from dual union all
select 4,'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
    a.cust_key
    , min(a.date_in) date_in
    , max(a.date_out) date_out
    , a.grp
    , sum(a.days) days
from
    (
select
    a.cust_key
    , a.date_in
    , a.date_out
    , sum(a.diff_tag) over ( partition by a.cust_key  order by a.cust_key,a.date_in,a.date_out ) grp
    , a.days
from
    (
SELECT cust_key, date_in, date_out,
               case when date_in - NVL
                  (MAX (date_out) OVER (partition by cust_key ORDER BY date_in, date_out ROWS 
                        BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING),
                   TO_DATE ('20010101', 'YYYYMMDD') - 1/24/60/60
                  )  > 1 then 1 else 0 end diff_tag
                  ,date_out - date_in + 1 days
          FROM t
    ) a
    ) a
group by
    a.cust_key
    , a.grp
having  sum(a.days) >= 7
김흥수(protokhs)님이 2015-02-04 10:09에 작성한 댓글입니다.
[Top]
No.
제목
작성자
작성일
조회
40707update 문 도와주세요 [2]
편두통
2015-02-02
8627
40706문자열 자르기 쿼리 관련 문의 [5]
돈방석
2015-02-02
7766
40705소수점 데이터가 있는지 확인 하는 방법 [3]
나루토21
2015-02-02
9843
407047일 연속 로그인한 고객데이터 뽑는 쿼리 부탁 드립니다. [2]
사바직자
2015-02-02
8106
40703DB 날짜 등록하기 [1]
조성찬
2015-02-02
7246
40702DB로 날짜 검색 [3]
조성찬
2015-01-30
8303
40701oracle로 1차 연립방정식 풀기 [2]
김흥수
2015-01-30
8678
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2024 DSN, All rights reserved.
작업시간: 0.030초, 이곳 서비스는
	PostgreSQL v16.2로 자료를 관리합니다