overlap sessions and counts
karma, March 18, 2011 - 4:04 pm UTC
Hi: In this post i dont see a way to indentify all overlaps within a set and count for each overlapping session. I have table with user weblogs where they have multiple sessions running. I need to find out each overlapping sessions and their respective counts.
Here is sample data :
select * from overlap_session where cust_id = '000000001'
CUST_ID SESS_START_TM SESS_END_TM LOAD_DT
000000001 01/04/2010 09:36:43 01/04/2010 19:42:37 01/04/2010
000000001 01/04/2010 09:42:00 01/04/2010 10:42:00 01/04/2010
000000001 01/04/2010 13:17:58 01/04/2010 15:05:39 01/04/2010
000000001 01/04/2010 15:48:40 01/04/2010 19:06:50 01/04/2010
000000001 01/04/2010 18:42:21 01/04/2010 22:10:35 01/04/2010
000000001 01/04/2010 21:15:27 01/04/2010 23:27:03 01/04/2010
000000001 01/04/2010 22:30:27 01/04/2010 23:15:03 01/04/2010
Expected Output
CUST_ID SESS_START_TM SESS_END_TM NUM_SESSIONS
000000001 01/04/2010 09:42:00 01/04/2010 10:42:00 2
000000001 01/04/2010 13:17:58 01/04/2010 15:05:39 2
000000001 01/04/2010 15:48:40 01/04/2010 18:42:20 2
000000001 01/04/2010 18:42:21 01/04/2010 19:06:50 3
000000001 01/04/2010 21:15:27 01/04/2010 22:10:35 2
000000001 01/04/2010 22:30:27 01/04/2010 23:15:03 2
scripts to load sample data:
CREATE TABLE OVERLAP_SESSION
(
CUST_WEB_ID VARCHAR2(36 BYTE),
CUST_ID VARCHAR2(9 BYTE),
SESSION_START_TMSTP DATE,
SESSION_END_TMSTP DATE,
LOAD_DT DATE,
LOAD_DT_KEY NUMBER(15)
)
SET DEFINE OFF;
Insert into OVERLAP_SESSION
(CUST_WEB_ID, CUST_ID, SESSION_START_TMSTP, SESSION_END_TMSTP, LOAD_DT,
LOAD_DT_KEY)
Values
('00000000-0000-0000-0000-000000000000', '000000001', TO_DATE('01/04/2010 09:36:43', 'MM/DD/YYYY HH24:MI:SS'),
TO_DATE('01/04/2010 19:42:37', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('01/04/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
2455201);
Insert into OVERLAP_SESSION
(CUST_WEB_ID, CUST_ID, SESSION_START_TMSTP, SESSION_END_TMSTP, LOAD_DT,
LOAD_DT_KEY)
Values
('00000000-0000-0000-0000-000000000000', '000000001', TO_DATE('01/04/2010 09:42:00', 'MM/DD/YYYY HH24:MI:SS'),
TO_DATE('01/04/2010 10:42:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('01/04/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
2455201);
Insert into OVERLAP_SESSION
(CUST_WEB_ID, CUST_ID, SESSION_START_TMSTP, SESSION_END_TMSTP, LOAD_DT,
LOAD_DT_KEY)
Values
('00000000-0000-0000-0000-000000000000', '000000001', TO_DATE('01/04/2010 13:17:58', 'MM/DD/YYYY HH24:MI:SS'),
TO_DATE('01/04/2010 15:05:39', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('01/04/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
2455201);
Insert into OVERLAP_SESSION
(CUST_WEB_ID, CUST_ID, SESSION_START_TMSTP, SESSION_END_TMSTP, LOAD_DT,
LOAD_DT_KEY)
Values
('00000000-0000-0000-0000-000000000000', '000000001', TO_DATE('01/04/2010 15:48:40', 'MM/DD/YYYY HH24:MI:SS'),
TO_DATE('01/04/2010 19:06:50', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('01/04/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
2455201);
Insert into OVERLAP_SESSION
(CUST_WEB_ID, CUST_ID, SESSION_START_TMSTP, SESSION_END_TMSTP, LOAD_DT,
LOAD_DT_KEY)
Values
('00000000-0000-0000-0000-000000000000', '000000001', TO_DATE('01/04/2010 18:42:21', 'MM/DD/YYYY HH24:MI:SS'),
TO_DATE('01/04/2010 22:10:35', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('01/04/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
2455201);
Insert into OVERLAP_SESSION
(CUST_WEB_ID, CUST_ID, SESSION_START_TMSTP, SESSION_END_TMSTP, LOAD_DT,
LOAD_DT_KEY)
Values
('00000000-0000-0000-0000-000000000000', '000000001', TO_DATE('01/04/2010 21:15:27', 'MM/DD/YYYY HH24:MI:SS'),
TO_DATE('01/04/2010 23:27:03', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('01/04/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
2455201);
Insert into OVERLAP_SESSION
(CUST_WEB_ID, CUST_ID, SESSION_START_TMSTP, SESSION_END_TMSTP, LOAD_DT,
LOAD_DT_KEY)
Values
('00000000-0000-0000-0000-000000000000', '000000001', TO_DATE('01/04/2010 22:30:27', 'MM/DD/YYYY HH24:MI:SS'),
TO_DATE('01/04/2010 23:15:03', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('01/04/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
2455201);
Insert into OVERLAP_SESSION
(CUST_WEB_ID, CUST_ID, SESSION_START_TMSTP, SESSION_END_TMSTP, LOAD_DT,
LOAD_DT_KEY)
Values
('11111111-2222-3333-4444-555555555555', '000000080', TO_DATE('11/22/2010 17:30:48', 'MM/DD/YYYY HH24:MI:SS'),
TO_DATE('11/22/2010 17:55:45', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('11/22/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
2455523);
Insert into OVERLAP_SESSION
(CUST_WEB_ID, CUST_ID, SESSION_START_TMSTP, SESSION_END_TMSTP, LOAD_DT,
LOAD_DT_KEY)
Values
('11111111-2222-3333-4444-555555555555', '000000080', TO_DATE('11/22/2010 17:35:32', 'MM/DD/YYYY HH24:MI:SS'),
TO_DATE('11/22/2010 17:35:54', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('11/22/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
2455523);
Insert into OVERLAP_SESSION
(CUST_WEB_ID, CUST_ID, SESSION_START_TMSTP, SESSION_END_TMSTP, LOAD_DT,
LOAD_DT_KEY)
Values
('11111111-2222-3333-4444-555555555555', '000000080', TO_DATE('11/22/2010 17:37:40', 'MM/DD/YYYY HH24:MI:SS'),
TO_DATE('11/22/2010 17:56:17', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('11/22/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
2455523);
Insert into OVERLAP_SESSION
(CUST_WEB_ID, CUST_ID, SESSION_START_TMSTP, SESSION_END_TMSTP, LOAD_DT,
LOAD_DT_KEY)
Values
('11111111-2222-3333-4444-555555555555', '000000080', TO_DATE('11/22/2010 17:56:59', 'MM/DD/YYYY HH24:MI:SS'),
TO_DATE('11/22/2010 18:40:05', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('11/22/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
2455523);
Insert into OVERLAP_SESSION
(CUST_WEB_ID, CUST_ID, SESSION_START_TMSTP, SESSION_END_TMSTP, LOAD_DT,
LOAD_DT_KEY)
Values
('11111111-2222-3333-4444-555555555555', '000000080', TO_DATE('11/22/2010 18:23:36', 'MM/DD/YYYY HH24:MI:SS'),
TO_DATE('11/22/2010 18:30:53', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('11/22/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
2455523);
Insert into OVERLAP_SESSION
(CUST_WEB_ID, CUST_ID, SESSION_START_TMSTP, SESSION_END_TMSTP, LOAD_DT,
LOAD_DT_KEY)
Values
('11111111-2222-3333-4444-555555555555', '000000080', TO_DATE('11/22/2010 18:32:59', 'MM/DD/YYYY HH24:MI:SS'),
TO_DATE('11/22/2010 18:39:16', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('11/22/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
2455523);
Insert into OVERLAP_SESSION
(CUST_WEB_ID, CUST_ID, SESSION_START_TMSTP, SESSION_END_TMSTP, LOAD_DT,
LOAD_DT_KEY)
Values
('11111111-2222-3333-4444-555555555555', '000000080', TO_DATE('11/22/2010 18:55:59', 'MM/DD/YYYY HH24:MI:SS'),
TO_DATE('11/22/2010 19:59:28', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('11/22/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
2455523);
Insert into OVERLAP_SESSION
(CUST_WEB_ID, CUST_ID, SESSION_START_TMSTP, SESSION_END_TMSTP, LOAD_DT,
LOAD_DT_KEY)
Values
('11111111-2222-3333-4444-555555555555', '000000080', TO_DATE('11/22/2010 19:32:24', 'MM/DD/YYYY HH24:MI:SS'),
TO_DATE('11/22/2010 20:46:59', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('11/22/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
2455523);
Insert into OVERLAP_SESSION
(CUST_WEB_ID, CUST_ID, SESSION_START_TMSTP, SESSION_END_TMSTP, LOAD_DT,
LOAD_DT_KEY)
Values
('11111111-2222-3333-4444-555555555555', '000000080', TO_DATE('11/22/2010 19:33:42', 'MM/DD/YYYY HH24:MI:SS'),
TO_DATE('11/22/2010 19:33:46', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('11/22/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
2455523);
Insert into OVERLAP_SESSION
(CUST_WEB_ID, CUST_ID, SESSION_START_TMSTP, SESSION_END_TMSTP, LOAD_DT,
LOAD_DT_KEY)
Values
('11111111-2222-3333-4444-555555555555', '000000080', TO_DATE('11/22/2010 19:56:57', 'MM/DD/YYYY HH24:MI:SS'),
TO_DATE('11/22/2010 19:58:14', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('11/22/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
2455523);
Insert into OVERLAP_SESSION
(CUST_WEB_ID, CUST_ID, SESSION_START_TMSTP, SESSION_END_TMSTP, LOAD_DT,
LOAD_DT_KEY)
Values
('11111111-2222-3333-4444-555555555555', '000000080', TO_DATE('11/22/2010 19:59:37', 'MM/DD/YYYY HH24:MI:SS'),
TO_DATE('11/22/2010 20:04:15', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('11/22/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
2455523);
Insert into OVERLAP_SESSION
(CUST_WEB_ID, CUST_ID, SESSION_START_TMSTP, SESSION_END_TMSTP, LOAD_DT,
LOAD_DT_KEY)
Values
('11111111-2222-3333-4444-555555555555', '000000080', TO_DATE('11/22/2010 20:22:31', 'MM/DD/YYYY HH24:MI:SS'),
TO_DATE('11/22/2010 20:24:31', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('11/22/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
2455523);
Insert into OVERLAP_SESSION
(CUST_WEB_ID, CUST_ID, SESSION_START_TMSTP, SESSION_END_TMSTP, LOAD_DT,
LOAD_DT_KEY)
Values
('11111111-2222-3333-4444-555555555555', '000000080', TO_DATE('11/22/2010 20:27:02', 'MM/DD/YYYY HH24:MI:SS'),
TO_DATE('11/22/2010 20:32:13', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('11/22/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
2455523);
Insert into OVERLAP_SESSION
(CUST_WEB_ID, CUST_ID, SESSION_START_TMSTP, SESSION_END_TMSTP, LOAD_DT,
LOAD_DT_KEY)
Values
('11111111-2222-3333-4444-555555555555', '000000080', TO_DATE('11/22/2010 20:34:08', 'MM/DD/YYYY HH24:MI:SS'),
TO_DATE('11/22/2010 20:42:47', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('11/22/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
2455523);
Insert into OVERLAP_SESSION
(CUST_WEB_ID, CUST_ID, SESSION_START_TMSTP, SESSION_END_TMSTP, LOAD_DT,
LOAD_DT_KEY)
Values
('11111111-2222-3333-4444-555555555555', '000000080', TO_DATE('11/22/2010 20:42:53', 'MM/DD/YYYY HH24:MI:SS'),
TO_DATE('11/22/2010 20:44:41', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('11/22/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
2455523);
Insert into OVERLAP_SESSION
(CUST_WEB_ID, CUST_ID, SESSION_START_TMSTP, SESSION_END_TMSTP, LOAD_DT,
LOAD_DT_KEY)
Values
('11111111-2222-3333-4444-555555555555', '000000080', TO_DATE('11/22/2010 20:45:21', 'MM/DD/YYYY HH24:MI:SS'),
TO_DATE('11/22/2010 20:45:37', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('11/22/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
2455523);
Insert into OVERLAP_SESSION
(CUST_WEB_ID, CUST_ID, SESSION_START_TMSTP, SESSION_END_TMSTP, LOAD_DT,
LOAD_DT_KEY)
Values
('11111111-2222-3333-4444-555555555555', '000000080', TO_DATE('11/22/2010 20:47:36', 'MM/DD/YYYY HH24:MI:SS'),
TO_DATE('11/22/2010 20:51:53', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('11/22/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
2455523);
Insert into OVERLAP_SESSION
(CUST_WEB_ID, CUST_ID, SESSION_START_TMSTP, SESSION_END_TMSTP, LOAD_DT,
LOAD_DT_KEY)
Values
('11111111-2222-3333-4444-555555555555', '000000080', TO_DATE('11/22/2010 20:48:18', 'MM/DD/YYYY HH24:MI:SS'),
TO_DATE('11/22/2010 21:02:59', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('11/22/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
2455523);
Insert into OVERLAP_SESSION
(CUST_WEB_ID, CUST_ID, SESSION_START_TMSTP, SESSION_END_TMSTP, LOAD_DT,
LOAD_DT_KEY)
Values
('11111111-2222-3333-4444-555555555555', '000000080', TO_DATE('11/22/2010 20:54:39', 'MM/DD/YYYY HH24:MI:SS'),
TO_DATE('11/22/2010 20:57:35', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('11/22/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
2455523);
Insert into OVERLAP_SESSION
(CUST_WEB_ID, CUST_ID, SESSION_START_TMSTP, SESSION_END_TMSTP, LOAD_DT,
LOAD_DT_KEY)
Values
('11111111-2222-3333-4444-555555555555', '000000080', TO_DATE('11/22/2010 20:58:42', 'MM/DD/YYYY HH24:MI:SS'),
TO_DATE('11/22/2010 21:00:57', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('11/22/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
2455523);
Insert into OVERLAP_SESSION
(CUST_WEB_ID, CUST_ID, SESSION_START_TMSTP, SESSION_END_TMSTP, LOAD_DT,
LOAD_DT_KEY)
Values
('11111111-2222-3333-4444-555555555555', '000000080', TO_DATE('11/22/2010 21:01:35', 'MM/DD/YYYY HH24:MI:SS'),
TO_DATE('11/22/2010 21:01:39', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('11/22/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
2455523);
Insert into OVERLAP_SESSION
(CUST_WEB_ID, CUST_ID, SESSION_START_TMSTP, SESSION_END_TMSTP, LOAD_DT,
LOAD_DT_KEY)
Values
('11111111-2222-3333-4444-555555555555', '000000080', TO_DATE('11/22/2010 21:02:33', 'MM/DD/YYYY HH24:MI:SS'),
TO_DATE('11/22/2010 21:27:41', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('11/22/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
2455523);
Insert into OVERLAP_SESSION
(CUST_WEB_ID, CUST_ID, SESSION_START_TMSTP, SESSION_END_TMSTP, LOAD_DT,
LOAD_DT_KEY)
Values
('11111111-2222-3333-4444-555555555555', '000000080', TO_DATE('11/22/2010 21:07:14', 'MM/DD/YYYY HH24:MI:SS'),
TO_DATE('11/22/2010 21:09:22', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('11/22/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
2455523);
Insert into OVERLAP_SESSION
(CUST_WEB_ID, CUST_ID, SESSION_START_TMSTP, SESSION_END_TMSTP, LOAD_DT,
LOAD_DT_KEY)
Values
('11111111-2222-3333-4444-555555555555', '000000080', TO_DATE('11/25/2010 00:22:27', 'MM/DD/YYYY HH24:MI:SS'),
TO_DATE('11/25/2010 00:28:39', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('11/25/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
2455526);
Insert into OVERLAP_SESSION
(CUST_WEB_ID, CUST_ID, SESSION_START_TMSTP, SESSION_END_TMSTP, LOAD_DT,
LOAD_DT_KEY)
Values
('11111111-2222-3333-4444-555555555555', '000000080', TO_DATE('11/25/2010 00:26:28', 'MM/DD/YYYY HH24:MI:SS'),
TO_DATE('11/25/2010 00:28:38', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('11/25/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
2455526);
Insert into OVERLAP_SESSION
(CUST_WEB_ID, CUST_ID, SESSION_START_TMSTP, SESSION_END_TMSTP, LOAD_DT,
LOAD_DT_KEY)
Values
('11111111-2222-3333-4444-555555555555', '000000080', TO_DATE('11/25/2010 00:49:54', 'MM/DD/YYYY HH24:MI:SS'),
TO_DATE('11/25/2010 00:55:53', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('11/25/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
2455526);
Insert into OVERLAP_SESSION
(CUST_WEB_ID, CUST_ID, SESSION_START_TMSTP, SESSION_END_TMSTP, LOAD_DT,
LOAD_DT_KEY)
Values
('11111111-2222-3333-4444-555555555555', '000000080', TO_DATE('11/25/2010 00:53:52', 'MM/DD/YYYY HH24:MI:SS'),
TO_DATE('11/25/2010 00:55:57', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('11/25/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
2455526);
COMMIT;
Grouping based on Year
snaseer, March 23, 2012 - 11:38 am UTC
Hello, When I found this Q&A I thought I can now solve my case but that is not true.
My data is like
CID RID Start_Year TITLE
52 1000 2001 1
52 1000 2002 1
52 1000 2003 6
52 1000 2004 6
52 1000 2005 6
52 1000 2007 1
52 1001 2003 1
52 1001 2004 1
52 1001 2005 1
52 1001 2006 3
52 1001 2007 3
Output I want to produce is
CID RID START-END TITLE
52 1000 2001-2002 1
52 1000 2003-2005 6
52 1000 2007-2007 1
52 1001 2003-2005 1
52 1001 2006-2007 3
Query I am come up with (output is not way I need)
select cid, rid, min(year2), max(year1), title
from (
select tc.cid, tc.rid, tc.title, tc.start_year year1, tp.start_year year2, decode (tc.title, tp.title, 'N', 'Y') gap
from t tc,
t tp
where tc.start_year -1 = tp.start_year
and tc.cid = tp.cid
and tc.rid = tp.rid
)
group by cid, rid, title, gap
--------
Table Script
create table T (
cid NUMBER,
rid NUMBER,
start_year NUMBER,
title NUMBER
);
insert into t (cid, rid, start_year, title) values (52, 1000, 2001, 1);
insert into t (cid, rid, start_year, title) values (52, 1000, 2002, 1);
insert into t (cid, rid, start_year, title) values (52, 1001, 2003, 1);
insert into t (cid, rid, start_year, title) values (52, 1000, 2003, 6);
insert into t (cid, rid, start_year, title) values (52, 1001, 2004, 1);
insert into t (cid, rid, start_year, title) values (52, 1000, 2004, 6);
insert into t (cid, rid, start_year, title) values (52, 1001, 2005, 1);
insert into t (cid, rid, start_year, title) values (52, 1000, 2005, 6);
insert into t (cid, rid, start_year, title) values (52, 1001, 2006, 3);
insert into t (cid, rid, start_year, title) values (52, 1001, 2007, 3);
insert into t (cid, rid, start_year, title) values (52, 1000, 2007, 1);
commit;
March 24, 2012 - 10:15 am UTC
I wish
a) you would have used the code button so that things lined up :(
b) actually explained your output. Why should I have to reverse engineer your output to try to figure out what you MIGHT have meant given your input????
So, fix it.