sql时间计算

时间:2016-01-15 11:21:19

标签: sql oracle

我在Oracle db中创建了以下sql并且它可以工作。现在我需要为每个阶段添加保持和取消保留时间,以便将状态更改为保持并以某种方式计算每个保持 - 未保留时间并从死线中删除时间 示例problemid:111阶段:分类复杂性:次要截止日期:2016年1月17日(2天)12:00让我们说我今天保持状态并且在8:00将其保留为tommorow结果必须是有问题的:111阶段:分类复杂性:次要截止日期:18/1/2016(2天)7:00。

所以我想计算暂停的时间并动态更改截止日期, 任何的想法?

select
ProblemID,
CASE --this calculate the next phase
         WHEN phases is null and INSERTIONDATE is null THEN 'Acknowledge'
         WHEN phases = 'Acknowledgement' and INSERTIONDATE is not null THEN 'Classification'
         WHEN phases = 'Classification' and INSERTIONDATE is not null THEN 'Feasibility'  
END
PHASES,  
CASE --this add the deadline for every phase
         WHEN phases is null and INSERTIONDATE is null and Priority = 1 THEN trunc(CreationDate)+2
         WHEN phases is null and INSERTIONDATE is null and Priority = 1  THEN trunc(CreationDate)+2
         WHEN phases is null and INSERTIONDATE is null and Priority = 1  THEN trunc(CreationDate)+2
         WHEN phases is null and INSERTIONDATE is null and Priority = 2  THEN trunc(CreationDate)+2
         WHEN phases is null and INSERTIONDATE is null and Priority = 2 THEN trunc(CreationDate)+2
         WHEN phases is null and INSERTIONDATE is null and Priority = 2  THEN trunc(CreationDate)+2
         WHEN phases = 'Acknowledgement' and INSERTIONDATE is not null and Priority = 1 and Complexity = 'minor' THEN trunc(InsertionDate)+5
         WHEN phases = 'Acknowledgement' and INSERTIONDATE is not null and Priority = 1 and Complexity = 'medium' THEN trunc(InsertionDate)+5
         WHEN phases = 'Acknowledgement' and INSERTIONDATE is not null and Priority = 1 and Complexity = 'major' THEN trunc(InsertionDate)+10
         WHEN phases = 'Acknowledgement' and INSERTIONDATE is not null and Priority = 2 and Complexity = 'minor' THEN 
          CASE
            WHEN (trunc(InsertionDate)+5-TRUNC(sysdate,'IW')+1) = 7 then trunc(InsertionDate)+6
            WHEN (trunc(InsertionDate)+5-trunc(sysdate,'IW')+1) = 6 then trunc(InsertionDate)+7
            END
         WHEN phases = 'Acknowledgement' and INSERTIONDATE is not null and Priority = 2 and Complexity = 'medium' THEN trunc(InsertionDate)+5
         WHEN phases = 'Acknowledgement' and INSERTIONDATE is not null and Priority = 2 and Complexity = 'major' THEN trunc(InsertionDate)+10
         WHEN phases = 'Classification' and INSERTIONDATE is not null and Priority = 1 and Complexity = 'minor' THEN trunc(InsertionDate)+5
         WHEN phases = 'Classification' and INSERTIONDATE is not null and Priority = 1 and Complexity = 'medium' THEN trunc(InsertionDate)+10
         WHEN phases = 'Classification' and INSERTIONDATE is not null and Priority = 1 and Complexity = 'major' THEN trunc(InsertionDate)+20
         WHEN phases = 'Classification' and INSERTIONDATE is not null and Priority = 2 and Complexity = 'minor' THEN trunc(InsertionDate)+10
         WHEN phases = 'Classification' and INSERTIONDATE is not null and Priority = 2 and Complexity = 'medium' THEN trunc(InsertionDate)+20
         WHEN phases = 'Classification' and INSERTIONDATE is not null and Priority = 2 and Complexity = 'major' THEN trunc(InsertionDate)+30 
END            
DEADLINE, 
CASE   --this calculate state       
        WHEN phases is null and INSERTIONDATE is null and Priority = 1 and Complexity = 'minor' THEN
        CASE
            WHEN (trunc(CreationDate)+3-TRUNC(sysdate,'IW')+1) = 7 THEN 'OK' 
            WHEN (trunc(CreationDate)+3-trunc(sysdate,'IW')+1) = 6 THEN 'OK' 
            ELSE 'DELAYED' 
        END
        WHEN phases is null and INSERTIONDATE is null and Priority = 1  and trunc(CreationDate)+3 <= trunc(SYSDATE) THEN 'DELAYED'          
        WHEN phases is null and INSERTIONDATE is null and Priority = 1  and trunc(CreationDate)+3 <= trunc(SYSDATE) THEN 'DELAYED'           
        WHEN phases is null and INSERTIONDATE is null and Priority = 2  and trunc(CreationDate)+3 <= trunc(SYSDATE) THEN 'DELAYED'        
        WHEN phases is null and INSERTIONDATE is null and Priority = 2  and trunc(CreationDate)+3 <= trunc(SYSDATE) THEN 'DELAYED'          
        WHEN phases is null and INSERTIONDATE is null and Priority = 2  and trunc(CreationDate)+3 <= trunc(SYSDATE) THEN 'DELAYED'
        WHEN phases = 'Acknowledge' and INSERTIONDATE is not null and Priority = 1 and Complexity = 'minor' and trunc(CreationDate)+6 <= trunc(SYSDATE) THEN 'DELAYED'
        WHEN phases = 'Acknowledge' and INSERTIONDATE is not null and Priority = 1 and Complexity = 'medium' and trunc(CreationDate)+6 <= trunc(SYSDATE) THEN 'DELAYED'
        WHEN phases = 'Acknowledge' and INSERTIONDATE is not null and Priority = 1 and Complexity = 'major' and trunc(CreationDate)+11 <= trunc(SYSDATE) THEN 'DELAYED'
        WHEN phases = 'Acknowledge' and INSERTIONDATE is not null and Priority = 2 and Complexity = 'minor' THEN 
         CASE
            WHEN (trunc(CreationDate)+5-TRUNC(sysdate,'IW')+1) = 7 THEN 'OK' 
            WHEN (trunc(CreationDate)+5-trunc(sysdate,'IW')+1) = 6 THEN 'OK' 
            ELSE 'DELAYED' 
        END
        WHEN phases = 'Acknowledge' and INSERTIONDATE is not null and Priority = 2 and Complexity = 'medium'  and trunc(CreationDate)+6 <= trunc(SYSDATE) THEN 'DELAYED'
        WHEN phases = 'Acknowledge' and INSERTIONDATE is not null and Priority = 2 and Complexity = 'major'  and trunc(CreationDate)+11 <= trunc(SYSDATE) THEN 'DELAYED'
        WHEN phases = 'Classification' and INSERTIONDATE is not null and Priority = 1 and Complexity = 'minor' and trunc(CreationDate)+6 <= trunc(SYSDATE) THEN 'DELAYED'
        WHEN phases = 'Classification' and INSERTIONDATE is not null and Priority = 1 and Complexity = 'medium' and trunc(CreationDate)+11 <= trunc(SYSDATE) THEN 'DELAYED'
        WHEN phases = 'Classification' and INSERTIONDATE is not null and Priority = 1 and Complexity = 'major' and trunc(CreationDate)+21 <= trunc(SYSDATE) THEN 'DELAYED'
        WHEN phases = 'Classification' and INSERTIONDATE is not null and Priority = 2 and Complexity = 'minor' and trunc(CreationDate)+11 <= trunc(SYSDATE) THEN 'DELAYED'
        WHEN phases = 'Classification' and INSERTIONDATE is not null and Priority = 2 and Complexity = 'medium' and trunc(CreationDate)+21 <= trunc(SYSDATE) THEN 'DELAYED'
        WHEN phases = 'Classification' and INSERTIONDATE is not null and Priority = 2 and Complexity = 'major' and trunc(CreationDate)+31 <= trunc(SYSDATE) THEN 'DELAYED'         
        ELSE 'OK'       
END
STATE
from problem_PHASES

0 个答案:

没有答案