从另一个 Case 语句引用 Case 语句中的标识符
Referencing identifier in Case statement from another Case statement
所以我有一个这样的案例陈述:(已编辑的实际报告代码)
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 语句,如下所示:
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,
这里的目标是查看我从第一个 case 语句中得到的输出,如果 'VA SLC' 或 'ABMC' 等标识符中的一个不是 在那里,然后它会把它放在自己的专栏中说 'VA SLC N/A'、'ABMC 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
或类似的内容。
如果您对我如何做到这一点有任何想法,请告诉我。谢谢!
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 版本中使用此语法。
所以我有一个这样的案例陈述:(已编辑的实际报告代码)
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 语句,如下所示:
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,
这里的目标是查看我从第一个 case 语句中得到的输出,如果 'VA SLC' 或 'ABMC' 等标识符中的一个不是 在那里,然后它会把它放在自己的专栏中说 'VA SLC N/A'、'ABMC 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
或类似的内容。
如果您对我如何做到这一点有任何想法,请告诉我。谢谢!
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 版本中使用此语法。