为SLA报告创建“是/否”列

时间:2013-08-06 14:07:20

标签: sql sql-server-2008 tsql

我极有可能用自己的想法走错了路,试图从中解脱,让我成为今天的我。在这里......寻求帮助。

我已经构建了一个很长的Case When语句,它告诉我何时必须完成交易日期,不包括周末并将其命名为“必需的SLA日期”

我要做的是创建一个列,说明是,事务是在SLA日期之前或之前完成的。或者否,它是在完成之后完成的。我的逻辑是使用下面的case statemtent,在另一个case语句中说“eventCompleteDate”是>需要SLA然后“是”否则“否”结束为MetSLA

  CASE WHEN Doc IN ( 'POS', 'Card' )
         THEN CAST(DATEADD(d, CASE DATEPART(dw, DateEntered)
                                WHEN 7 THEN 2
                                WHEN 1 THEN 1
                                ELSE 0
                              END, DateEntered) + ( DATEPART(dw,
                                                          DATEADD(d,
                                                          CASE DATEPART(dw,
                                                          DateEntered)
                                                          WHEN 7 THEN 2
                                                          WHEN 1 THEN 1
                                                          ELSE 0
                                                          END, DateEntered))
                                                    - 2 + 5 ) % 5
              + ( ( DATEPART(dw, DATEADD(d, CASE DATEPART(dw, DateEntered)
                                              WHEN 7 THEN 2
                                              WHEN 1 THEN 1
                                              ELSE 0
                                            END, DateEntered)) - 2 + 5 )
                  / 5 ) * 7 - ( DATEPART(dw,
                                         DATEADD(d,
                                                 CASE DATEPART(dw,
                                                          DateEntered)
                                                   WHEN 7 THEN 2
                                                   WHEN 1 THEN 1
                                                   ELSE 0
                                                 END, DateEntered)) - 2 ) AS DATE)
         ELSE CAST(DATEADD(d, CASE DATEPART(dw, DateEntered)
                                WHEN 7 THEN 2
                                WHEN 1 THEN 1
                                ELSE 0
                              END, DateEntered) + ( DATEPART(dw,
                                                          DATEADD(d,
                                                          CASE DATEPART(dw,
                                                          DateEntered)
                                                          WHEN 7 THEN 2
                                                          WHEN 1 THEN 1
                                                          ELSE 0
                                                          END, DateEntered))
                                                    - 2 + 10 ) % 5
              + ( ( DATEPART(dw, DATEADD(d, CASE DATEPART(dw, DateEntered)
                                              WHEN 7 THEN 2
                                              WHEN 1 THEN 1
                                              ELSE 0
                                            END, DateEntered)) - 2 + 10 )
                  / 5 ) * 7 - ( DATEPART(dw,
                                         DATEADD(d,
                                                 CASE DATEPART(dw,
                                                          DateEntered)
                                                   WHEN 7 THEN 2
                                                   WHEN 1 THEN 1
                                                   ELSE 0
                                                 END, DateEntered)) - 2 ) AS DATE)
    END AS 'Required SLA Date'     

2 个答案:

答案 0 :(得分:2)

我会使用嵌套查询(或CTE)来避免两次写入逻辑。

SELECT RequiredSlaDate, 
       case when RequiredSlaDate > EventCompleteDate then 'NO' 
            else 'YES' 
       end as MetSLA
FROM
(
   SELECT EventCompleteDate, 
          CASE WHEN Doc -- etc, etc ...
          AS RequiredSlaDate
   FROM Table
 ) as Source

答案 1 :(得分:2)

以下是CTE解决方案的示例。您到目前为止的整个查询都在CTE内部,从CTE中选择只需添加一个新列:

WITH CTE AS 
(
   SELECT CASE WHEN Doc IN ( 'POS', 'Card' )
               THEN .... --your whole CASE from above goes here, I will not copy/paste
          END AS [Required SLA Date],
     eventCompleteDate,
     YourOtherColumns -- also all other columns you are selecting in your query
   FROM
   YourTable
)
SELECT *,
   CASE WHEN eventCompleteDate > [Required SLA Date] THEN 'Yes' ELSE 'No' END AS MetSLA
FROM CTE

<强> SQLFiddle VERY Simplified DEMO

PS:另请注意,在将列命名为AS 'Required SLA Date'时,不应使用引号,而应使用方括号AS [Required SLA Date]。如果没有别的可读性。