SQL应用参数而不是硬编码

时间:2010-11-01 17:23:12

标签: sql reporting-services parameters business-intelligence

我需要一点帮助,我有一份报告说我正在运行,只会根据一些硬编码的“类别”提取某些信息。但是,我想尽可能灵活地使用它,并且我希望用用户可以选择的参数替换这些编码类别。但是当我尝试这个时,它永远不会因各种原因而起作用目前的代码是:

    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_categoryofparticipant',见下文:

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'

这显然不起作用,但如果有人能指出我正确的方向,那将非常感激。

由于

1 个答案:

答案 0 :(得分:2)

您不需要在SUM或查询结尾处使用HAVING - 查询结尾处的HAVING可以更改为WHERE,而SUM可以是:

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'