Reputation: 413
We have processes which connect to a subsystem and we are tracking them by this timestamp. What I would like to find out is the maximum number of processes have connected in a time interval of N minutes.
This time interval is the TIMEOUT interval of a process so the countdown of the elapsed time starts when it connects. So the interval is a "floating" interval.
For simplicity, the length of this interval is 5 minutes, the connection times are in minutes and not in milliseconds.
We already have a PL/SQL function to calculate this number but I would like to know if it is possible to do it using only an SQL.
Example:
09:10 09:15 09:20 09:25 09:30
| | | | |
----|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|---
| | | | |
| | | | | | | | | | |
a b c a c a b c d a f <-- PROCESSES
f b <-- PROCESSES
Here is SQL for the PROCESSES table:
create table PRCS
(
prc_id NUMBER(12) not null,
prc_name VARCHAR2(25) not null
);
alter table PRCS add constraint PRCS_PK primary key (PRC_ID);
alter table PRCS add constraint PRCS_UK unique (PRC_NAME);
insert into PRCS values('1', 'a');
insert into PRCS values('2', 'b');
insert into PRCS values('3', 'c');
insert into PRCS values('4', 'd');
insert into PRCS values('5', 'e');
insert into PRCS values('6', 'f');
Here is SQL for the CONNECTIONS table:
create table CON_JOURNAL
(
con_id NUMBER(12) not null,
con_date TIMESTAMP(6) not null,
con_prc NUMBER(12) not null
)
;
alter table CON_JOURNAL add constraint CON_PK primary key (CON_ID);
alter table CON_JOURNAL add constraint CON_UK unique (CON_DATE, CON_PRC);
alter table CON_JOURNAL add constraint CON_PRC_FK foreign key (CON_PRC)
references PRCS (PRC_ID);
insert into CON_JOURNAL values( '1', to_date('2013.01.09 09:12', 'yyyy.mm.dd hh24:mi'), '1');
insert into CON_JOURNAL values( '2', to_date('2013.01.09 09:13', 'yyyy.mm.dd hh24:mi'), '2');
insert into CON_JOURNAL values( '3', to_date('2013.01.09 09:14', 'yyyy.mm.dd hh24:mi'), '3');
insert into CON_JOURNAL values( '4', to_date('2013.01.09 09:18', 'yyyy.mm.dd hh24:mi'), '1');
insert into CON_JOURNAL values( '5', to_date('2013.01.09 09:19', 'yyyy.mm.dd hh24:mi'), '3');
insert into CON_JOURNAL values( '6', to_date('2013.01.09 09:21', 'yyyy.mm.dd hh24:mi'), '1');
insert into CON_JOURNAL values( '7', to_date('2013.01.09 09:22', 'yyyy.mm.dd hh24:mi'), '2');
insert into CON_JOURNAL values( '8', to_date('2013.01.09 09:23', 'yyyy.mm.dd hh24:mi'), '3');
insert into CON_JOURNAL values( '9', to_date('2013.01.09 09:24', 'yyyy.mm.dd hh24:mi'), '4');
insert into CON_JOURNAL values('10', to_date('2013.01.09 09:24', 'yyyy.mm.dd hh24:mi'), '6');
insert into CON_JOURNAL values('11', to_date('2013.01.09 09:26', 'yyyy.mm.dd hh24:mi'), '1');
insert into CON_JOURNAL values('12', to_date('2013.01.09 09:26', 'yyyy.mm.dd hh24:mi'), '2');
insert into CON_JOURNAL values('13', to_date('2013.01.09 09:29', 'yyyy.mm.dd hh24:mi'), '6');
Thank you for your help,
SK
Upvotes: 2
Views: 408
Reputation: 8361
Interesting question! :-)
To sum up connections within the 5 minute time window, you could use the windowing clause of an analytic function. For instance, you can order the connections of a single process (PARITITION BY con_prc
) by connection time (ORDER BY con_date
) and limit the window to the preceding 5 minutes (RANGE INTERVAL '5' MINUTE PRECEDING
):
SELECT con_prc ,
COUNT(*) OVER (PARTITION BY con_prc
ORDER BY con_date
RANGE INTERVAL '5' MINUTE PRECEDING) AS connections
FROM con_journal
Then, depending on your requirements, select the maximum number of connections per process:
SELECT con_prc, MAX(connections) AS max_con
FROM (
SELECT con_prc ,
COUNT(*) OVER (PARTITION BY con_prc
ORDER BY con_date
RANGE INTERVAL '5' MINUTE PRECEDING) AS connections
FROM con_journal
)
GROUP BY con_prc;
1 2
2 2
3 2
4 1
6 2
Edit: Alternatively, you can create a table of time slices to join the meaurements that fall within a time slice:
SELECT to_char(t,'HH24:MI') as t, count(con_id) as connected_sessions
FROM con_journal j
JOIN (SELECT TIMESTAMP '2013-01-09 09:00:00' + (level/24/60) as t
FROM dual CONNECT BY level < 60)
ON t BETWEEN con_date AND con_date + INTERVAL '5' MINUTE
GROUP BY t
ORDER BY t;
Upvotes: 1