0

下面是我SQL用来计算两天之间天数的查询

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

我需要在计算 CompDays 时排除 FRIDAY & SATURDAY

提前感谢您的帮助。

4

1 回答 1

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
于 2013-05-07T05:49:58.853 回答