sql ssrs如何将变量与表参数

时间:2015-10-16 13:16:36

标签: sql-server reporting-services parameters

我的报告需要一个参数,该参数可能包含一个值或一组值(分配给地区办事处的司法管辖区)。我无法弄清楚如果特定值(a.jurisdiction)与设置为表的参数中包含的值匹配,如何询问sql。我有以下代码:

DECLARE @District INT = 1614 --Richmond D1
DECLARE @StartDate DATETIME = '20160101'
DECLARE @EndDate DATETIME = '20160731'


DECLARE @Jurisdiction TABLE(Location INT) --= 1223 --Richmond City --multiselect?
IF @District = 1614 --Richmond District 1
INSERT INTO @Jurisdiction Values (1223); --Richmond City
IF @District = 1632 --Newport News District 19
INSERT INTO @Jurisdiction Values (1203); --Newport News
IF @District = 1642 --Fairfax District 29
INSERT INTO @Jurisdiction Values (2568) --Fairfax City
,(1154) ,(1140) ,(1178) ,(1243) 
SELECT b.OffenderId
    , b.Sex
    , b.CurrentLocationId
    , b.MasterTermId
    , b.ReleaseDate
    , b.Jurisdiction
    , b.HomePlanAddress
    FROM (SELECT DISTINCT o.OffenderId
            , o.CurrentLocationId
            , CASE WHEN o.GenderId = 12 THEN 'M'
                WHEN o.GenderId = 13 THEN 'F'
                ELSE 'Unknown' END AS Sex
            , mt.MasterTermId
            , t.TermId 
            , CASE WHEN t.GtrdApprovedDate IS NULL AND
            t.MprdApprovedDate IS NULL THEN '19000101'
        WHEN t.GtrdApprovedDate IS NULL THEN t.MprdApprovedDate
        WHEN t.MprdApprovedDate IS NULL THEN t.GtrdApprovedDate
        WHEN t.MprdApprovedDate < t.GtrdApprovedDate THEN 
                          t.MprdApprovedDate
        ELSE t.GtrdApprovedDate END AS ReleaseDate
        , j.HomePlanAddress
        , j.Jurisdiction    
        FROM ind.Offender AS o
        INNER JOIN (SELECT oa.OffenderId
        , oa.AddressId
        , LTRIM(COALESCE(CAST(a.StreetNumber AS 
                    VARCHAR(10)),' ') + COALESCE(a.StreetNumberSuffix
                    + ' ',' '  
                    + COALESCE(a.StreetName + ',','') + COALESCE(' ' 
                    + a.AppartmentNumber + ',','')
        + l.LocationName + ',' + 'VA ' + COALESCE
                    (a.ZipCode,'')) AS HomePlanAddress
        , j.LocationName AS Jurisdiction
    FROM ind.Offender_Address AS oa
        INNER JOIN ref.Address AS a ON oa.AddressId =
                              a.AddressId
    LEFT JOIN ref.Location AS l ON a.CountyId = l.LocationId
    INNER JOIN ref.Location AS j ON a.JurisdictionId = j.LocationId
    WHERE a.StateId = 1047
    AND EXISTS (SELECT a.JurisdictionId FROM @Jurisdiction)
    --AND a.JurisdictionId IN (@Jurisdiction) --
    AND oa.AddressTypeId = 5 --Proposed
    AND oa.EndDate IS NULL) AS j ON o.OffenderId = j.OffenderId
INNER JOIN scl.MasterTerm AS mt ON o.OffenderId = mt.OffenderId
LEFT JOIN scl.Term AS t ON mt.MasterTermId = t.MasterTermId
WHERE o.CurrentLocationId IS NOT NULL
AND t.TermStatusId <> 631) AS b --Inactive
WHERE b.ReleaseDate BETWEEN @StartDate AND @EndDate

我试图找到解决方案,但大多数都是指使用我无法在我们的环境中使用的存储过程。我在找到的一个答案上尝试了EXISTS选项,但它给了我所有位置,而不仅仅是参数中的位置。有什么建议吗?

对基于数据集的答案SSRS多值参数过滤器的引用似乎并未解决区域参数与用于管辖权的值没有一对一匹配的事实。但它给了我一些工作和玩耍的东西。

1 个答案:

答案 0 :(得分:0)

我不知道为什么

AND a.JurisdictionId IN (@Jurisdiction)

不适合你。据我所知,应该这样做。

您可以尝试将过滤器添加到SSRS 数据集,而不是在查询中。为表达式管辖区参数选择管辖区作为

enter image description here