ID | | | VISIT_DATE |
1 | | | 2014-01-13 |
2 | | | 2014-01-20 |
1 | | | 2014-01-17 |
1 | | | 2014-01-17 |
2 | | | 2014-01-20 |
2 | | | 2014-01-20 |
2 | | | 2014-01-21 |
1 | | | 2014-01-21 |
1 | | | 2014-01-23 |
2 | | | 2014-01-17 |
3 | | | 2014-01-20 |
3 | | | 2014-01-20 |
3 | | | 2014-01-20 |
3 | | | 2014-01-21 |
WITH t AS ( SELECT 1 id, '2014-01-13' visit_date FROM dual UNION ALL SELECT 2, '2014-01-20' FROM dual UNION ALL SELECT 1, '2014-01-17' FROM dual UNION ALL SELECT 1, '2014-01-17' FROM dual UNION ALL SELECT 2, '2014-01-20' FROM dual UNION ALL SELECT 2, '2014-01-20' FROM dual UNION ALL SELECT 2, '2014-01-21' FROM dual UNION ALL SELECT 1, '2014-01-21' FROM dual UNION ALL SELECT 1, '2014-01-23' FROM dual UNION ALL SELECT 2, '2014-01-17' FROM dual UNION ALL SELECT 3, '2014-01-20' FROM dual UNION ALL SELECT 3, '2014-01-20' FROM dual UNION ALL SELECT 3, '2014-01-20' FROM dual UNION ALL SELECT 3, '2014-01-21' FROM dual ) , t1 AS ( SELECT id , TO_DATE(visit_date, 'yyyy-mm-dd') visit_date FROM t ) SELECT id , visit_date , visit_date - LAG(visit_date) OVER(PARTITION BY id ORDER BY visit_date) term , COUNT(*) OVER(PARTITION BY id ORDER BY visit_date, ROWNUM) cnt FROM t1 ;
SELECT VISIT_RETURN, SUM(GAP) AS RETURN_GAP, COUNT(*) AS RETURN_CNT FROM( SELECT VISIT_TIME, VISIT_RETURN, (TO_DATE(VISIT_RETURN)-TO_DATE(VISIT_TIME)) AS GAP FROM( SELECT TO_CHAR(VISIT_TIME, 'YYYY/MM/DD') AS VISIT_TIME, TO_CHAR(LEAD(VISIT_TIME, 1, VISIT_TIME) OVER(PARTITION BY ID_PC ORDER BY ID_VISITOR), 'YYYY/MM/DD') AS VISIT_RETURN FROM CI_VISITOR_INFO WHERE TO_DATE(TO_CHAR(visit_time, 'YYYY/MM/DD'),'YYYY/MM/DD') BETWEEN TO_DATE ( '2014/01/13' , 'YYYY/MM/DD' ) AND TO_DATE ( '2014/01/21' , 'YYYY/MM/DD' ) ) WHERE (TO_DATE(VISIT_RETURN)-TO_DATE(VISIT_TIME)) != 0 ) GROUP BY VISIT_RETURN이렇게 ..해보았는데요, 이렇게 해도 괜찮을까요 ??