SQL应用参数而不是硬编码
我需要一点帮助,我有一份报告,我正在运行,它只提取一些基于硬编码的“类别”的特定信息。但是,我希望使其尽可能灵活,并且我希望用用户可以选择的参数替换这些编码的类别。然而,当我尝试这样做时,由于各种原因,它从来都不起作用。目前的代码是:SQL应用参数而不是硬编码,sql,reporting-services,parameters,business-intelligence,Sql,Reporting Services,Parameters,Business Intelligence,我需要一点帮助,我有一份报告,我正在运行,它只提取一些基于硬编码的“类别”的特定信息。但是,我希望使其尽可能灵活,并且我希望用用户可以选择的参数替换这些编码的类别。然而,当我尝试这样做时,由于各种原因,它从来都不起作用。目前的代码是: SELECT gcs_allocatedpdaidname AS PDA, gcs_consideringapplyingyearname AS 'Intending to Start ITT', SUM
SELECT gcs_allocatedpdaidname AS PDA,
gcs_consideringapplyingyearname AS 'Intending to Start ITT',
SUM(CASE WHEN (gcs_allocatedpdaid IS NOT NULL) AND (gcs_ContactType = 1) THEN 1 ELSE 0 END) AS 'Total Participants',
SUM(CASE WHEN (gcs_allocatedpdaid IS NOT NULL) AND (gcs_ContactType = 1) AND (StatusCode = 1) THEN 1 ELSE 0 END) AS 'Active Participants',
SUM(CASE WHEN (gcs_allocatedpdaid IS NOT NULL) AND (gcs_ContactType = 1) AND (StatusCode = 200001) THEN 1 ELSE 0 END) AS 'Completed Participants',
SUM(CASE WHEN (gcs_allocatedpdaid IS NOT NULL) AND (gcs_ContactType = 1) AND ((gcs_categoryofparticipant = 7) OR
(gcs_categoryofparticipant = 8) OR
(gcs_categoryofparticipant = 9) OR
(gcs_categoryofparticipant = 10) OR
(gcs_categoryofparticipant = 11) OR
(gcs_categoryofparticipant = 12)) THEN 1 ELSE 0 END) AS 'On ITT and beyond TOTAL',
SUM(CASE WHEN (gcs_allocatedpdaid IS NOT NULL) AND (gcs_ContactType = 1) AND ((StatusCode = 1) OR (StatusCode = 200001)) THEN 1 ELSE 0 END) AS 'On ITT and beyond ACTIVE/COMPLETE'
FROM FilteredContact
GROUP BY gcs_allocatedpdaidname, gcs_allocatedpdaid, gcs_consideringapplyingyearname
HAVING (gcs_allocatedpdaidname IN (@PDA)) AND (gcs_consideringapplyingyearname IN (@ITTYear))
ORDER BY PDA, 'Intending to Start ITT'
我要替换的硬编码是“gcs_类别参与者”,见下文:
SUM(CASE WHEN (gcs_allocatedpdaid IS NOT NULL) AND (gcs_ContactType = 1) AND
((gcs_categoryofparticipant = 7) OR
(gcs_categoryofparticipant = 8) OR
(gcs_categoryofparticipant = 9) OR
(gcs_categoryofparticipant = 10) OR
(gcs_categoryofparticipant = 11) OR
(gcs_categoryofparticipant = 12)) THEN 1 ELSE 0 END) AS 'On ITT and beyond TOTAL'
我想应该是这样的:
SUM(CASE WHEN (gcs_allocatedpdaid IS NOT NULL) AND (gcs_ContactType = 1) AND (HAVING (gcs_categoryofparticipant IN (@Category))) THEN 1 ELSE 0 END) AS 'On ITT and beyond TOTAL'
这显然不起作用,但如果有人能给我指出正确的方向,我将不胜感激
谢谢您不需要在总和中或在查询结束时拥有-在查询结束时拥有可以更改为WHERE,而总和可以是:
SUM(CASE WHEN (gcs_allocatedpdaid IS NOT NULL) AND
(gcs_ContactType = 1) AND
(gcs_categoryofparticipant IN (@Category))
THEN 1
ELSE 0
END) AS 'On ITT and beyond TOTAL'
@ck,您可以在reporting services中使用多值参数,这就是多值参数的工作方式。