定义时间条件的案例

时间:2017-05-19 20:04:17

标签: sql sql-server

我想在以下声明中显示JobTime:

SELECT DISTINCT
CASE WHEN [PRIORITY_TYPE_NAME] = 'Urgent' AND ISNULL(JobTime,0) <=240 THEN 'NOC Service Request Resolution - Urgent'
    WHEN [PRIORITY_TYPE_NAME] = 'High' AND ISNULL(JobTime,0) <=720 THEN 'NOC Service Request Resolution - High'
    WHEN [PRIORITY_TYPE_NAME] = 'Medium' AND ISNULL(JobTime,0) <=1440 THEN 'NOC Service Request Resolution - Medium'
    WHEN [PRIORITY_TYPE_NAME] = 'Low' AND ISNULL(JobTime,0) <=2880 THEN 'NOC Service Request Resolution - Low'
END  AS [Problem_Type_Name]

JobTime计算如下:

CASE WHEN (case when [Escalated to Resolved time] is null then (ISNULL([Escalated to Resolved time],0) + ISNULL([Escalated to Closed time],0)- ISNULL([hold time],0)) else (ISNULL([Escalated to Closed time],0) - ISNULL([hold time],0)) end) <0THEN 0 ELSE (case when [Escalated to Resolved time] is null then (ISNULL([Escalated to Resolved time],0) + ISNULL([Escalated to Closed time],0)- ISNULL([hold time],0)) else (ISNULL([Escalated to Closed time],0) - ISNULL([hold time],0)) end)  END AS JobTime

是否有可能或简单的方法在select中使用JobTime?

1 个答案:

答案 0 :(得分:0)

您可以将您的案例陈述放入外部申请中,这样您就可以在select语句中引用该字段

SELECT DISTINCT
CASE WHEN [PRIORITY_TYPE_NAME] = 'Urgent' AND ISNULL(JobTime,0) <=240 THEN 'NOC Service Request Resolution - Urgent'
     WHEN [PRIORITY_TYPE_NAME] = 'High' AND ISNULL(JobTime,0) <=720 THEN 'NOC Service Request Resolution - High'
     WHEN [PRIORITY_TYPE_NAME] = 'Medium' AND ISNULL(JobTime,0) <=1440 THEN 'NOC Service Request Resolution - Medium'
     WHEN [PRIORITY_TYPE_NAME] = 'Low' AND ISNULL(JobTime,0) <=2880 THEN 'NOC Service Request Resolution - Low'
END  AS [Problem_Type_Name], 
oa.JobTime

FROM dbo.TableName (NOLOCK) t
OUTER APPLY ( 
                  SELECT CASE WHEN (case when [Escalated to Resolved time] is null then (ISNULL([Escalated to Resolved time],0) + ISNULL([Escalated to Closed time],0)- ISNULL([hold time],0)) else (ISNULL([Escalated to Closed time],0) - ISNULL([hold time],0)) end) <0THEN 0 ELSE (case when [Escalated to Resolved time] is null then (ISNULL([Escalated to Resolved time],0) + ISNULL([Escalated to Closed time],0)- ISNULL([hold time],0)) else (ISNULL([Escalated to Closed time],0) - ISNULL([hold time],0)) end)  END AS JobTime
            ) oa