我正在使用下面的SQL查询创建SSRS报告作为报告的单个数据集。如您所见,我有不同的Where子句,将根据运行报表时选择的参数下拉列表使用这些子句。如果我只需要将单个值传递给单个值,我可以使用下拉列表的选定值。但是,似乎我需要从下拉列表中传递一个虚拟值,然后根据该选择,执行过滤器Switch语句并确定要使用哪个where子句。我可以根据选择的下拉列表将SQL字符串传递给我的数据集吗?我还有什么方法可以实现这个目标?
SELECT DISTINCT
cip.CaseID
,cip.ProsNum AS FileNum
,cip.CaseInvPersLastName + ', ' + cip.CaseInvPersFirstName AS Attorney
,c.CaseStatusCode
,c.CaseStatusDesc
,dbo.ConvertTimeToClientTimeZone(ce.EventDT) AS OpenDate
,cip2.CaseInvPersLastName AS Respondent
,j.Dispo
,cd.Description AS DispoDesc
FROM jw50_Case c
INNER JOIN jw50_CaseInvPers cip
ON cip.CaseID = c.CaseID AND cip.InvolveTypeCode = 'CP007' AND cip.CaseInvPersActive = 1
INNER JOIN jw50_CaseInvPers cip2
ON cip2.CaseID = c.CaseID AND cip2.InvolveTypeCode = 'CP001' AND cip2.CaseInvPersActive = 1
INNER JOIN jw50_CaseEvent ce
ON ce.EventID = (SELECT TOP 1 EventID FROM jw50_CaseEvent WHERE CaseID = cip.CaseID ORDER BY EventDT)
LEFT JOIN devJudgement j
ON j.CaseID = cip.CaseID
LEFT JOIN devCountDispo cd
ON cd.Code = j.Dispo
-- Sent to screening
--WHERE c.CaseStatusCode = 'CS007'
-- Closed
WHERE c.caseStatusCode = 'CS006' AND j.Dispo IN ('CD002','CD003') AND c.CaseTypeCode = 'TY001'
-- Closed - Info Only
--WHERE c.caseStatusCode = 'CS006' AND j.Dispo IN ('CD001') AND c.CaseTypeCode = 'TY001'
-- Reopened
--WHERE c.caseStatusCode = 'CS008' AND c.CaseTypeCode = 'TY001'
-- Under appeal
--WHERE c.caseStatusCode = 'CS009' AND c.CaseTypeCode = 'TY001'
AND ce.EventDT >= @startDate AND ce.EventDT < @endDate + 1
Order BY cip.ProsNum
请提供任何帮助。感谢。
答案 0 :(得分:2)
使用括号创建条件组以及OR
,以便对于传递的变量的任何值,您可以使用一个查询并使用正确的WHERE
条件,例如:
WHERE ((@variable = 1 AND c.CaseStatusCode = 'CS007')
OR (@variable = 2 AND c.caseStatusCode = 'CS006' AND j.Dispo IN ('CD002','CD003') AND c.CaseTypeCode = 'TY001')
OR (@variable = 3 AND c.caseStatusCode = 'CS006' AND j.Dispo IN ('CD001') AND c.CaseTypeCode = 'TY001')
OR (@variable = 4 AND c.caseStatusCode = 'CS008' AND c.CaseTypeCode = 'TY001')
OR (@variable = 5 AND c.caseStatusCode = 'CS009' AND c.CaseTypeCode = 'TY001'))
AND ce.EventDT >= @startDate AND ce.EventDT < @endDate + 1
在上述情况下,必须满足其中一个@variable = n
条件,并根据需要调整带有日期条件的底线。