下面是用于计算两天之间天数的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
我要求 排除FRIDAY&在计算CompDays时的星期六 。
提前感谢您的帮助。
答案 0 :(得分: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