0

很有可能我的想法走错了路,而试图从中解脱出来让我走到了今天。在这里……寻求帮助。

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

我要做的是创建一个列,上面写着,是的,交易是在 SLA 日期或之前完成的。或否,它是在之后完成的。我的逻辑是使用下面的案例声明,在另一个案例声明中说当“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'     
4

2 回答 2

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
于 2013-08-06T14:22:30.420 回答
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 非常简化的演示

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

于 2013-08-06T15:06:48.217 回答