/* CREATING TABLE */
create table T_USER_INCIDENT
(
ID number
,STAT number(1)
,USER_ID varchar2(15)
,INCIDENT_DATE date
)
/*================================*/
/* CREATING SEQENCE */
create sequence SQ_USER_INCIDENT
start with 1
increment by 1
nomaxvalue;
/*================================*/
/* CREATE TRIGGER FOR ID INSERT */
create trigger TRIG_USER_INCIDENT
before insert on T_USER_INCIDENT
for each row
begin
select SQ_USER_INCIDENT.nextval into :new.id from dual;
end;
/
/*================================*/
/* INSERTING GIVEN SAMPLE DATA */
INSERT INTO T_USER_INCIDENT (STAT,USER_ID,INCIDENT_DATE) VALUES (1,'USER1',TO_DATE('18.08.2010 13:10:14','DD.MM.YYYY HH24:MI:SS'));
INSERT INTO T_USER_INCIDENT (STAT,USER_ID,INCIDENT_DATE) VALUES (2,'USER1',TO_DATE('18.08.2010 15:15:40','DD.MM.YYYY HH24:MI:SS'));
INSERT INTO T_USER_INCIDENT (STAT,USER_ID,INCIDENT_DATE) VALUES (1,'USER1',TO_DATE('18.08.2010 16:15:10','DD.MM.YYYY HH24:MI:SS'));
INSERT INTO T_USER_INCIDENT (STAT,USER_ID,INCIDENT_DATE) VALUES (2,'USER1',TO_DATE('18.08.2010 18:35:32','DD.MM.YYYY HH24:MI:SS'));
INSERT INTO T_USER_INCIDENT (STAT,USER_ID,INCIDENT_DATE) VALUES (1,'USER2',TO_DATE('18.08.2010 07:05:46','DD.MM.YYYY HH24:MI:SS'));
INSERT INTO T_USER_INCIDENT (STAT,USER_ID,INCIDENT_DATE) VALUES (2,'USER2',TO_DATE('18.08.2010 13:10:01','DD.MM.YYYY HH24:MI:SS'));
INSERT INTO T_USER_INCIDENT (STAT,USER_ID,INCIDENT_DATE) VALUES (1,'USER1',TO_DATE('19.08.2010 09:01:10','DD.MM.YYYY HH24:MI:SS'));
INSERT INTO T_USER_INCIDENT (STAT,USER_ID,INCIDENT_DATE) VALUES (2,'USER1',TO_DATE('19.08.2010 16:15:19','DD.MM.YYYY HH24:MI:SS'));
INSERT INTO T_USER_INCIDENT (STAT,USER_ID,INCIDENT_DATE) VALUES (1,'USER2',TO_DATE('19.08.2010 11:02:56','DD.MM.YYYY HH24:MI:SS'));
INSERT INTO T_USER_INCIDENT (STAT,USER_ID,INCIDENT_DATE) VALUES (2,'USER2',TO_DATE('19.08.2010 18:45:22','DD.MM.YYYY HH24:MI:SS'));
/*================================*/
/* CHECKING INSERTED DATA */
SELECT * FROM T_USER_INCIDENT
/*===================================================*/
/* OUT PUT OF ABOVE QUERY */
/*===================================================*/
/* 1 1 1 USER1 8/18/2010 1:10:14 PM */
/* 2 2 2 USER1 8/18/2010 3:15:40 PM */
/* 3 3 1 USER1 8/18/2010 4:15:10 PM */
/* 4 4 2 USER1 8/18/2010 6:35:32 PM */
/* 5 5 1 USER2 8/18/2010 7:05:46 AM */
/* 6 6 2 USER2 8/18/2010 1:10:01 PM */
/* 7 7 1 USER1 8/19/2010 9:01:10 AM */
/* 8 8 2 USER1 8/19/2010 4:15:19 PM */
/* 9 9 1 USER2 8/19/201011:02:56 AM */
/* 10 10 2 USER2 8/19/2010 6:45:22 PM */
/*===================================================*/
/*CREATING FUNCTION TO RETURN RESPECTED CLOSING TIME OF INCIDENT WITH GIVEN USER_ID */
CREATE OR REPLACE FUNCTION FN_GET_INCIDENT_END_TIME
(P_USER_ID IN T_USER_INCIDENT.USER_ID%TYPE,
P_INCIDENT_START_TIME T_USER_INCIDENT.INCIDENT_DATE%TYPE)
RETURN T_USER_INCIDENT.INCIDENT_DATE%TYPE
IS
V_INCIDENT_CLOSE_TIME T_USER_INCIDENT.INCIDENT_DATE%TYPE;
BEGIN
SELECT MIN(CLOSE_TIMES)
INTO V_INCIDENT_CLOSE_TIME
FROM
(SELECT A.INCIDENT_DATE AS CLOSE_TIMES
FROM T_USER_INCIDENT A
WHERE A.USER_ID=P_USER_ID AND A.INCIDENT_DATE>P_INCIDENT_START_TIME AND A.STAT=2);
RETURN V_INCIDENT_CLOSE_TIME;
END FN_GET_INCIDENT_END_TIME;
/*================================*/
/* MAIN QUERY FOR OUT PUT */
SELECT USER_ID as "USER",TRUNC(INCIDENT_DATE) as "DATE",to_char(to_date('00:00:00','HH24:MI:SS') + SUM(DURATION), 'HH24:MI:SS') as "DURATION"
FROM
(SELECT
UI.USER_ID
,UI.INCIDENT_DATE
,FN_GET_INCIDENT_END_TIME(UI.USER_ID,UI.INCIDENT_DATE) AS END_TIME
,NVL(FN_GET_INCIDENT_END_TIME(UI.USER_ID,UI.INCIDENT_DATE),SYSDATE)-UI.INCIDENT_DATE AS DURATION
FROM T_USER_INCIDENT UI
WHERE STAT=1)
GROUP BY TRUNC(INCIDENT_DATE),USER_ID
/*================================*/
/*=======================================*/
/* OUT PUT OF ABOVE QUERY */
/*=======================================*/
/* 1 USER1 8/18/2010 04:25:48 */
/* 2 USER2 8/18/2010 06:04:15 */
/* 3 USER1 8/19/2010 07:14:09 */
/* 4 USER2 8/19/2010 07:42:26 */
/*=======================================*/