0

我有一份报告,其中有一列包含不同的状态。如何从只有东和西而不是单个状态的参数中过滤这些状态?

我对状态列表的查询是:

select distinct
state
from contract

我对参数的查询是:

select distinct
case when  STATE = 'CA' or STATE = 'WA' or STATE = 'MO'
        or STATE = 'OR' or STATE = 'NV' or STATE = 'ID'
        or STATE = 'MT' or STATE = 'WY' or STATE = 'UT'
        or STATE = 'AZ' or STATE = 'CO' or STATE = 'NM'
        or STATE = 'ND' or STATE = 'SD' or STATE = 'NE'
        or STATE = 'KS' or STATE = 'OK' or STATE = 'TX'
        or STATE = 'LA' or STATE = 'AR' or STATE = 'IA'
        or STATE = 'MN' or STATE = 'HI' or STATE = 'AK' then 'West'

    when   STATE = 'MI' or STATE = 'OH' or STATE = 'PA'
        or STATE = 'NJ' or STATE = 'WI' or STATE = 'IL'
        or STATE = 'IN' or STATE = 'KY' or STATE = 'TN'
        or STATE = 'MS' or STATE = 'AL' or STATE = 'GA' 
        or STATE = 'FL' or STATE = 'SC' or STATE = 'NC'
        or STATE = 'VA' or STATE = 'WV' or STATE = 'MD'
        or STATE = 'DE' or STATE = 'CT' or STATE = 'RI' 
        or STATE = 'NY' or STATE = 'MA' or STATE = 'VT'
        or STATE = 'NH' or STATE = 'ME'  then 'East'
    else null end as 'Territory Selection'
from contracts

我的参数将显示为 [All][West][East]。我想如果我在 [Territory Selection] 中将过滤器设置为 [State],它会将各州过滤为属于东部或西部,但它没有按预期工作。任何指针将不胜感激!

4

1 回答 1

2
  1. 使用复选框“允许空”设置名为“EastWest”的参数。设置一个简单的查询以获取“来自查询的可用值”的数据,例如:

    Select 'West' as Territory
    union
    Select 'East'
    
  2. 像上面一样创建另一个数据集,但将其放入 CTE 并引用第一个参数,如下所示:

    With a as 
    (
    Select 
        State
    , case when  STATE in ('CA','WA', 'MO', 'OR' ,'NV' ,'ID','MT' ,'WY' ,'UT','AZ' ,'CO' ,'NM','ND' ,'SD' ,'NE'
            ,'KS' ,'OK' ,'TX','LA' ,'AR' ,'IA','MN' ,'HI' ,'AK') then 'West'
        when  State in ('MI' ,'OH' ,'PA','NJ' ,'WI' ,'IL','IN' ,'KY' ,'TN','MS' ,'AL' ,'GA','FL' ,'SC' ,'NC'
            ,'VA' ,'WV' ,'MD','DE' ,'CT' ,'RI','NY' ,'MA' ,'VT','NH' ,'ME')  then 'East'
        end as 'Territory Selection'
    from (State Listing)
    )
    Select State
    from a
    where [Territory Selection] = isnull(@EastWest,[Territory Selection])
    
  3. 请注意谓词,因为它本质上说:“当变量为空时,通过将列与自身相关联来选择所有内容”

  4. 创建另一个“隐藏”变量,将其称为“状态”,并将其绑定到您创建的上述数据集。您基本上是从某个小变量中获取一个变量,然后从之前列中不存在的实际数据中获取您需要的数据,或者……您需要一个不同的标签,而不是为每个状态实例列出的数据。

  5. 您现在可能有一个常规数据集,例如

    Select (thing)
    from (real table)
    where states in (@states)
    
于 2013-05-08T21:30:40.643 回答