0

我目前正在尝试使用具有日期范围(DateTO 和 DateFrom)的过滤器构建报告。问题是报告需要具有可选日期范围的能力。以下是我当前的 SQL。请告知我如何修改

SELECT 

PG.Name AS [Project Group],
PSG.Name AS [Project Sub Group],
P.ReferenceNumber,
P.ReferenceNumber + ' / ' + P.ProjectTitle AS [Project Description],
CASE WHEN W.Removed = 1
 THEN 'Yes'
 ELSE 'No'
END AS [Removed],
W.Description,
W.CommunicationStartDate,
PR.Name as [Person Responsible],


CASE WHEN W.CommunicationStartDate IS NULL
 THEN 'Not Specified'
 ELSE CONVERT(NVARCHAR, W.CommunicationStartDate, 103)
END AS [Deadline],
ES.Name AS [Status],
CASE WHEN W.Estimate IS NOT NULL
 THEN W.Estimate
 ELSE 0
END AS [Estimate]

FROM dbo.ProjectWorkplan W
  INNER JOIN dbo.Project P
   INNER JOIN dbo.ProjectSubGroup PSG
   INNER JOIN dbo.ProjectGroup PG ON PSG.ProjectGroupId = PG.ProjectGroupId
  ON P.ProjectSubGroupId = PSG.ProjectSubGroupId
 ON W.ProjectId = P.ProjectId
 INNER JOIN dbo.PersonResponsible PR ON W.PersonResponsibleId = PR.PersonResponsibleId
 INNER JOIN dbo.ElementStatus ES ON W.ElementStatusId = ES.ElementStatusId

WHERE P.DateCompleted IS NULL AND W.Removed = 0
AND W.CommunicationStartDate BETWEEN @DateFrom AND @DateTo


AND ES.NAME in (@ActionRequired, @FollowUp, @OnHold, @UrgentAction, @Waiting,@Complete)


ORDER BY PG.Name, PSG.Name, P.ReferenceNumber, W.Removed, W.CommunicationStartDate
4

1 回答 1

1

您可以检查 NULL 的日期参数并在只有它们两个都有效的情况下使用它们:

AND (@DateFrom IS NULL OR @DateTo IS NULL OR W.CommunicationStartDate BETWEEN @DateFrom AND @DateTo)

于 2013-09-05T01:47:31.063 回答