2013-11-27 45 views
0

我有我的表如下。使用Oracle分析函數處理時間計算

create table AuditInfo 
    (AuditID number(5), 
    TaskID number(5), 
    Task_Status varchar2(15), 
    UserID number(5), 
    SegmentTime number(5)); 

insert into AuditInfo values(1,101,'ASSIGNED',1,0); 
insert into AuditInfo values(2,101,'HOLD',1,5); 
insert into AuditInfo values(3,101,'RESERVED',1,0); 
insert into AuditInfo values(4,101,'ASSIGNED',1,0); 
insert into AuditInfo values(5,101,'COMPLETED',1,5); 
insert into AuditInfo values(6,102,'ASSIGNED',1,0); 
insert into AuditInfo values(7,102,'HOLD',1,5); 
insert into AuditInfo values(8,102,'TRANSFER',2,0); 
insert into AuditInfo values(9,102,'ASSIGNED',2,0); 
insert into AuditInfo values(10,102,'HOLD',2,5); 
insert into AuditInfo values(11,102,'COMPLETED',2,0); 

我需要爲每個任務一個額外的列Task_Handle_Time在其生命週期

1)與狀態轉換的任務的結束或完成的被視爲生活cyle結束。 2)如果任務同時包含TRANSFER(用戶傳輸))或COMPLETED,則需要分別計算每個生命週期的句柄時間。 TaskID 102代表這種情況。

3)Total Handle時間是從ASSIGNED狀態開始到其生命週期狀態(TRANSFER,COMPLETED)的任務段的總和。

我的輸出應該像

AuditID TaskID Task_Status UserID SegmentTime HandleTime 

1  101 ASSIGNED 1  0   0 

2  101 HOLD  1  5   0 

3  101 RESERVED 1  0   0 

4  101 ASSIGNED 1  0   0 

5  101 COMPLETED 1  5   10 

6  102 ASSIGNED 1  0   0 

7  102 HOLD  1  5   0 

8  102 TRANSFER 2  0   5 

9  102 ASSIGNED 2  0   0 

10  102 HOLD  2  5   0 

11  102 COMPLETED 2  0   5 

在上述輸出任務101沒有用戶傳送和處理時間與各段時間和用於 任務102所有總和計算一旦有用戶轉自UserID 1到2,所以我們應該爲每個生命週期計算兩次HandleTime。

這裏是我的查詢需要

select ai.*, case when row_number() over(partition by TaskID,Task_Status in(TRANSFER,COMPLETED) order by TaskID)=count(*) over(partition by TaskID,Task_Status in(TRANSFER,COMPLETED) order by TaskID) 
then sum(SegmentTime) over(partition by TaskID,Task_Status in(TRANSFER,COMPLETED) order by TaskID) else 0 end as "Handle Time" from AuditInfo ai order by AuditID; 

有沒有辦法來實現這一目標,其不工作?

這是我試過的SQL小提琴鏈接。 http://www.sqlfiddle.com/#!4/8d96d/7

回答

0

我覺得這個做的伎倆 - 關鍵是超過0的正常行和「1」使用Oracle的總和()解析函數的磨合總過渡一行。這使您可以劃分適合兩個轉換之間的行。稍微清理一下,以解決該方法的偏差錯誤,並且您是黃金版:

SELECT AUDITID, TASKID, TASK_STATUS, USERID, 
     DECODE(RUNTOT,GRP,0,SUM(SEGMENTTIME) OVER (PARTITION BY TASKID, GRP)) HANDLING 
FROM (
    SELECT A.*, 
      NVL(LAG(RUNTOT) OVER (PARTITION BY TASKID ORDER BY AUDITID) , RUNTOT) GRP 
    FROM ( 
     SELECT A.*, 
       SUM(CASE WHEN TASK_STATUS IN ('TRANSFER', 'COMPLETED') THEN 1 ELSE 0 END) 
         OVER (PARTITION BY TASKID ORDER BY AUDITID) RUNTOT 
     FROM AUDITINFO A) A) 
ORDER BY AUDITID ASC 

AUDITID TASKID TASK_STATUS  USERID HANDLING        
1  101 ASSIGNED  1  0          
2  101 HOLD   1  0          
3  101 RESERVED  1  0          
4  101 ASSIGNED  1  0          
5  101 COMPLETED  1  10          
6  102 ASSIGNED  1  0          
7  102 HOLD   1  0          
8  102 TRANSFER  2  5          
9  102 ASSIGNED  2  0          
10  102 HOLD   2  0          
11  102 COMPLETED  2  5          
0

它可以只是一個簡單的sum()而不是一個複雜的嵌套分析函數來實現一個適當的數據模型。這是你可以採取的方向。

create table Task ( 
    TaskID  number(5)  primary key 
); 
create table TaskCycle ( 
    CycleID  number(5)  primary key, 
    TaskId  number(5)  not null references Task (TaskID) 
); 
create table CycleSegment (
    SegmentID number(5)  primary key, 
    CycleID  number(5)  not null references TaskCycle (CycleID), 
    Task_Status varchar2(15) not null, 
    UserID  number(5)  not null, 
    SegmentTime number(5)  not null 
); 

insert into Task values (101); 
insert into Task values (102); 

insert into TaskCycle values (1, 101); 
insert into TaskCycle values (2, 102); 
insert into TaskCycle values (3, 102); 

insert into CycleSegment values (1, 1, 'ASSIGNED', 1, 0); 
insert into CycleSegment values (2, 1, 'HOLD', 1, 5); 
insert into CycleSegment values (3, 1, 'RESERVED', 1, 0); 
insert into CycleSegment values (4, 1, 'ASSIGNED', 1, 0); 
insert into CycleSegment values (5, 1, 'COMPLETED', 1, 5); 
insert into CycleSegment values (6, 2, 'ASSIGNED', 1, 0); 
insert into CycleSegment values (7, 2, 'HOLD', 1, 5); 
insert into CycleSegment values (8, 2, 'TRANSFER', 2, 0); 
insert into CycleSegment values (9, 3, 'ASSIGNED', 2, 0); 
insert into CycleSegment values (10, 3, 'HOLD', 2, 5); 
insert into CycleSegment values (11, 3, 'COMPLETED', 2, 0); 

select s.SegmentID, c.TaskID, s.Task_Status, s.UserID, s.SegmentTime 
    , case 
      when s.Task_Status in ('TRANSFER', 'COMPLETED') 
      then 
      (select sum(s2.SegmentTime) 
       from CycleSegment s2 
       where s2.CycleID = c.CycleID) 
      else 0 
     end as Handle_Time 
    from TaskCycle c 
    join CycleSegment s on (s.CycleID = c.CycleID) 
order by c.TaskID, s.SegmentID 
;