我有我的表如下。使用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