我的表格如下。
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)状态为TRANSFER或COMPLETED的任务被视为生命终结cyle。
2)如果任务包含TRANSFER(用户转移))或COMPLETED,则需要为每个生命周期分别计算处理时间。 TaskID 102代表这种情况。
3)总处理时间是从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 Fiddle链接。 http://www.sqlfiddle.com/#!4/8d96d/7
答案 0 :(得分: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
;
答案 1 :(得分:0)
我认为这样做很有用 - 关键是使用Oracle的sum()分析函数作为正常行的运行总计超过0,对于转换行使用“1”。这允许您划分适合两个转换之间的行。一点清理来处理这种方法的一个一个错误,你就是金色的:
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