2010-11-01 38 views
2

我需要一点帮助,我有一个报告,我正在运行,只根据硬编码的许多'类别'提取某些信息。但是,我希望尽可能灵活,并且我想用可以由用户选择的参数替换这些编码类别。但是,当我尝试这个时,它从来不会出于各种原因。当前的代码是:SQL应用参数而不是硬编码

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' 

这显然不工作,但如果有人能指出我在正确的方向,将不胜感激。

感谢

回答

2

你不需要具有任何的总和,或在查询的结尾 - 在查询结束的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' 
+0

除了不能将数组传递给单个参数并以这种方式使用它... – cjk 2010-11-01 17:44:09

+1

@ck,您可以在报告服务中 - 这就是多值参数的工作方式。 – 2010-11-01 17:45:57

+0

抱歉错过了报道位... – cjk 2010-11-02 08:34:48

相关问题