2013-04-28 82 views
0

使用DATEDIFF函數下面是我用來計算天的第二日排除週五和週六,而在SSRS

SELECT  count(*) as NoofCalls, outside.ReferenceNo, outside.WorkflowType, outside.Department, inside.CompletedDate, outside.CommentResponse, outside.CommentText, outside.CommentUserDisplayName, outside.StepName, outside.Originator, outside.Subject, outside.StartTime, CAST(DATEDIFF(hh, outside.StartTime, inside.CompletedDate)/24.0 AS decimal(10,5)) AS CompDays 
FROM 
(
    SELECT  ReferenceNo, MAX(CommentDate) as CompletedDate 
    FROM  FNCUSTOM.dbo.WorkflowHistory 
    WHERE  WorkflowType IN (@WorkflowType) AND Department IN (@Department) AND Originator IN (@Originator) AND ReferenceNo IN (@Reference_No) 
       AND (StartTime between @StartDate AND @EndDate) 
    GROUP BY ReferenceNo 
) inside 
INNER JOIN FNCUSTOM.dbo.WorkflowHistory outside ON outside.ReferenceNo = inside.ReferenceNo AND outside.CommentDate=inside.CompletedDAte 
GROUP BY outside.ReferenceNo, outside.WorkflowType, outside.Department, inside.CompletedDate, outside.CommentResponse, outside.CommentText, outside.CommentUserDisplayName, outside.StepName, outside.Originator, outside.Subject, outside.StartTime 

我需要對排除之間FRIDAY &週六同時calulating CompDaysSQL查詢。

非常感謝您的幫助。

+0

使用SSRS 2005 起始日期可能會在週四開始(比如2013年4月25日08:58:47.000)和結束日期(比如2013年4月28日20:58: 47.000),那麼COMPDAYS應該是1.5天。不包括**完整的星期五和星期六** – 2013-04-28 07:52:40

+0

您可能期望在兩個日期之間有多大的天數 - 這個時間總是相對較小(比如不到一週),或者可能是幾周/幾個月/幾年? – 2013-04-28 08:03:05

+0

@WillA它可以不到一週;並可以超過一個星期....很少有可能去幾個月..但不是幾年 – 2013-04-28 09:50:50

回答

0

修正自己,

SELECT  count(*) as NoofCalls, outside.ReferenceNo, outside.WorkflowType, outside.Department, inside.CompletedDate, outside.CommentResponse, outside.CommentText, 
    outside.CommentUserDisplayName, outside.StepName, outside.Originator, outside.Subject, outside.StartTime, 
          CAST(DATEDIFF(hh, outside.StartTime, inside.CompletedDate)/24.0 AS decimal(10,5)) 
          -(DATEDIFF(wk, outside.StartTime, inside.CompletedDate) * 2) 
          -(CASE WHEN DATENAME(dw, outside.StartTime) = 'Friday' THEN 1 ELSE 0 END) 
          -(CASE WHEN DATENAME(dw, inside.CompletedDate) = 'Saturday' THEN 1 ELSE 0 END) AS CompDays 
    FROM 
(
      SELECT  ReferenceNo, MAX(CommentDate) as CompletedDate 
      FROM  FNCUSTOM.dbo.WorkflowHistory 
      WHERE  WorkflowType IN (@WorkflowType) AND Department IN (@Department) AND Originator IN (@Originator) AND ReferenceNo IN (@Reference_No) 
        AND (StartTime between @StartDate AND @EndDate) 
      GROUP BY ReferenceNo 
) inside 
INNER JOIN FNCUSTOM.dbo.WorkflowHistory outside ON outside.ReferenceNo = inside.ReferenceNo AND outside.CommentDate=inside.CompletedDAte 
GROUP BY    outside.ReferenceNo, outside.WorkflowType, outside.Department, inside.CompletedDate, outside.CommentResponse, outside.CommentText, 
    outside.CommentUserDisplayName, outside.StepName, outside.Originator, outside.Subject, outside.StartTime 
ORDER BY    outside.ReferenceNo, outside.WorkflowType, outside.Department, outside.StartTime, inside.CompletedDate, outside.CommentResponse, outside.CommentText, 
    outside.CommentUserDisplayName, outside.StepName, outside.Originator, outside.Subject 
相關問題