在给定特定SLA天的列中创建不包括周末的截止日期

时间:2013-08-08 13:06:14

标签: sql sql-server-2008 tsql

我有一个“DaysUntilDue”列,说明何时需要完成文档。我需要使用“记录日”列根据“到期日”计算“到期日”,并排除星期六和星期日。

我所拥有的只是我知道我需要工作的专栏。我有一个Case When Statement的代码,它排除了Sat和Sun,但不确定如何将其纳入我需要做的事情。我拥有我需要的所有列,并且所有列都按照他们需要的方式链接。真的很挣扎如何制作一个声明,说明从文件的“logTime”使用现有的“DaysUntilDue”计算截止日期,但排除周末

SELECT     
CategoryName, 
DocCategory,
DaysUntilDue, 
TranNbr, 
DueDate, 
LogTime
FROM Queue INNER JOIN WQMTransactions ON Queue.TranNbr = WQMTransactions.TranNbr INNER  JOIN 
 ServiceQ INNER JOIN CategoryList ON ServiceQn.TranID =      CategoryList.TranID 
 INNER JOIN Category ON CategoryList.CategoryID =     Category.CategoryID 
 ON Queue.CategoryID = Category.CategoryID AND Queue.TranID = ServiceQ.TranID
也许是因为睡眠不足,但从概念上讲,我无法确定如何做到这一点。

我假设我需要使用这个我为另一个qry做类似的Case语句。

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' 

1 个答案:

答案 0 :(得分:2)

我试图纠正您的查询。还计算了截止日期。当DaysUntilDue = 0时,截止日期为空。

SELECT     
CategoryName, 
DocCategory,
DaysUntilDue, 
TranNbr, 
LogTime,
dateadd(day, a.n, LoggedDay) DueDate
FROM Queue 
INNER JOIN WQMTransactions ON Queue.TranNbr = WQMTransactions.TranNbr 
INNER JOIN ServiceQ ON Queue.CategoryID = Category.CategoryID AND Queue.TranID = ServiceQ.TranID
INNER JOIN CategoryList ON ServiceQn.TranID = CategoryList.TranID 
INNER JOIN Category ON CategoryList.CategoryID = Category.CategoryID 
cross apply
(
select coalesce(max(number)+1, 0) n
from(
select top (DaysUntilDue) number from 
master..spt_values
WHERE TYPE = 'P'
and datediff(day, -number-1, LoggedDay) % 7 not in (5,6)
order by number
) 
a) 
a