从另一个Case语句引用Case语句中的标识符

时间:2015-07-16 14:33:50

标签: sql oracle case alias

所以我有一个案例陈述,如下所示:(编辑实际的报告代码)

select
     case
        when t1.reportcode in ('x', 'y', 'z') then 'AFMC'
        when t2.reportcode in ('x', 'y', 'z') then 'AFMC'
        when t1.reportcode in ('x', 'y', 'z') then 'FOH'
        when t2.reportcode in ('x', 'y', 'z') then 'FOH'
        when t1.reportcode in ('x', 'y', 'z') then 'Forest Service'
        when t2.reportcode in ('x', 'y', 'z') then 'Forest Service'
        when t1.reportcode in ('x', 'y', 'z') then 'HHS-Strive'
        when t2.reportcode in ('x', 'y', 'z') then 'HHS-Strive'
        when t1.reportcode in ('x', 'y', 'z') then 'NASA'
        when t2.reportcode in ('x', 'y', 'z') then 'NASA'
        when t1.reportcode in ('x', 'y', 'z') then 'VA SLC'
        when t2.reportcode in ('x', 'y', 'z') then 'VA SLC'
        when t1.reportcode in ('x', 'y', 'z') then 'ABMC'
        when t2.reportcode in ('x', 'y', 'z') then 'ABMC'
        when t1.reportcode in ('x', 'y', 'z') then 'DFAS'
        when t2.reportcode in ('x', 'y', 'z') then 'DFAS'
        when t1.reportcode in ('x', 'y', 'z') then 'DON'
        when t2.reportcode in ('x', 'y', 'z') then 'DON'
     end as FirstGroups,

检查所有这些代码并在代码匹配时为其提供标识符(ABMC,AFMC等)。

在此之后,我有另一个案例陈述,如下所示:

case
        when 'ABMC' not in FirstGroups then 'ABMC N/A'
        when 'AFMC' not in FirstGroups then 'AFMC N/A'
        when 'DFAS' not in FirstGroups then 'DFAS N/A'
        when 'DON' not in FirstGroups then 'DON N/A'
        when 'FOH' not in FirstGroups then 'FOH N/A'
        when 'Forest Service' not in FirstGroups then 'Forest Service N/A'
        when 'HHS-Strive' not in FirstGroups then 'HHS-Strive N/A'
        when 'NASA' not in FirstGroups then 'NASA N/A'
        when 'VA SLC' not in FirstGroups then 'VA SLC N/A'
 end as NotApplicable,

这里的目标是查看我从第一个案例陈述中得到的输出,以及其中一个标识符,例如' VA SLC'或者' ABM​​C'那里的不是,那么它会把它放在自己的专栏中,说'' VA SLC N / A' ABM​​C N / A'等等...不幸的是我无法在我的第二个case语句中引用第一个case语句中的FirstGroups标识符。我尝试使用t1.reportcode和t2.reportcode,但那些没有用。

预期输出将如下所示:

FirstGroups            NotApplicable
ABMC                   AFMC N/A
DFAS                   NASA N/A
DON
FOH
Forest Service
NASA
VA SLC

或类似的东西。

如果您对我如何做到这一点有任何想法,请告诉我。谢谢!

1 个答案:

答案 0 :(得分:1)

not in FirstGroups建议您尝试将该列中的所有值视为集合,并从所有组的列表中排除该集合的成员以形成第二列;这与同一行的第一列中的值无关。您的示例输出似乎支持。

这似乎是客户端应该处理的事情 - 您查询获取第一组,并知道(或单独查询)所有组的列表,并让客户端正确显示。

你可以在SQL中做到这一点,它有点乱,特别是不能使用CTE。

这使用两个内联视图;第一个获得您已经拥有的firstgroups值,但我已根据您的预期输出添加了不同的值;第二个通过使用固定的组名列表获取不适用的值,并排除firstgroups中的值。不幸的是,因为您不能使用CTE来表示重复原始案例陈述以进行排除。

每个内联视图都会添加一个分析排名列,因此示例中的firstgroups会获得排名为1-7的行,而第二行会获得排名为1-2的行。

然后将这两个内联视图外部连接,并将该等级作为连接条件。

select fg.groupname as firstgroups, na.notapplicable
from (
  select groupname, dense_rank() over (order by groupname) as rnk
  from (
    select distinct case
        when t1.reportcode in (1, 2, 3) then 'AFMC'
        when t1.reportcode in (4, 5, 6) then 'FOH'
        when t1.reportcode in (7, 8, 9) then 'Forest Service'
        when t1.reportcode in (10, 11, 12) then 'HHS-Strive'
        when t1.reportcode in (13, 14, 15) then 'NASA'
        when t1.reportcode in (16, 17, 18) then 'VA SLC'
        when t1.reportcode in (19, 20, 21) then 'ABMC'
        when t1.reportcode in (22, 23, 24) then 'DFAS'
        when t1.reportcode in (25, 26, 27) then 'DON'
      end as groupname
    from t1
  )
) fg
full outer join (
  select column_value || ' N/A' as notapplicable,
    dense_rank() over (order by column_value) as rnk
  from table(sys.odcivarchar2list('ABMC', 'AFMC', 'DFAS', 'DON', 'FOH',
    'Forest Service', 'HHS-Strive', 'NASA', 'VA SLC')
  ) allgroups
  where not exists (
    select 1
    from (
      select case
        when t1.reportcode in (1, 2, 3) then 'AFMC'
        when t1.reportcode in (4, 5, 6) then 'FOH'
        when t1.reportcode in (7, 8, 9) then 'Forest Service'
        when t1.reportcode in (10, 11, 12) then 'HHS-Strive'
        when t1.reportcode in (13, 14, 15) then 'NASA'
        when t1.reportcode in (16, 17, 18) then 'VA SLC'
        when t1.reportcode in (19, 20, 21) then 'ABMC'
        when t1.reportcode in (22, 23, 24) then 'DFAS'
        when t1.reportcode in (25, 26, 27) then 'DON'
      end as groupname
      from t1
    ) firstgroups
    where firstgroups.groupname = allgroups.column_value
  )
) na
on na.rnk = fg.rnk
order by coalesce(fg.rnk, na.rnk);

获得:

FIRSTGROUPS          NOTAPPLICABLE      
-------------------- --------------------
ABMC                 AFMC N/A            
DFAS                 NASA N/A            
DON                                      
FOH                                      
Forest Service                           
HHS-Strive                               
VA SLC                                                            

SQL Fiddle demo。我已编制了报告代码值以显示您显示的匹配项。

CTE的时间有点短:

with firstgroups as (
  select groupname, dense_rank() over (order by groupname) as rnk
  from (
    select distinct case
        when t1.reportcode in (1, 2, 3) then 'AFMC'
        when t1.reportcode in (4, 5, 6) then 'FOH'
        when t1.reportcode in (7, 8, 9) then 'Forest Service'
        when t1.reportcode in (10, 11, 12) then 'HHS-Strive'
        when t1.reportcode in (13, 14, 15) then 'NASA'
        when t1.reportcode in (16, 17, 18) then 'VA SLC'
        when t1.reportcode in (19, 20, 21) then 'ABMC'
        when t1.reportcode in (22, 23, 24) then 'DFAS'
        when t1.reportcode in (25, 26, 27) then 'DON'
      end as groupname
    from t1
  )
)
select fg.groupname as firstgroups, na.notapplicable
from firstgroups fg
full outer join (
  select column_value || ' N/A' as notapplicable,
    dense_rank() over (order by column_value) as rnk
  from table(sys.odcivarchar2list('ABMC', 'AFMC', 'DFAS', 'DON', 'FOH',
    'Forest Service', 'HHS-Strive', 'NASA', 'VA SLC')
  ) allgroups
  where not exists (
    select 1
    from firstgroups
    where firstgroups.groupname = allgroups.column_value
  )
) na
on na.rnk = fg.rnk
order by coalesce(fg.rnk, na.rnk);

SQL Fiddle demo了解相关信息,因为您似乎无法在您的SSIS版本中使用此语法。