【发布时间】:2015-07-16 14:33:49
【问题描述】:
所以我有一个看起来像这样的案例陈述:(编辑的实际报告代码)
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”等...不幸的是,我无法在第二个案例语句的第一个案例语句中引用该 FirstGroups 标识符。我也尝试过使用 t1.reportcode 和 t2.reportcode,但这些都不起作用。
预期的输出如下所示:
FirstGroups NotApplicable
ABMC AFMC N/A
DFAS NASA N/A
DON
FOH
Forest Service
NASA
VA SLC
或类似的东西。
如果您对我如何做到这一点有任何想法,请告诉我。谢谢!
【问题讨论】:
-
“不在”这里应该做什么? FirstGroups 是一个标量值,所以它与这里的不等于相同,这意味着您只能得到 'ABMC N/A' 或 'AFMC N/A' - 其余情况永远无法达到?
-
在第二种情况下引用第一种情况的别名是重复的of this question;但这似乎只是问题的一部分。
-
好吧,目的是检查 FirstGroups 案例输出列中是否包含“ABMC”或其他任何内容。如果该列中没有任何内容,则应在另一列中输出“ABMC N/A”等。听起来在这里使用“不在”是不正确的。
-
不在任何行的列中?您可以分析地做到这一点,但您仍然只会为整个集合(或分区)显示一个 N/A 值。也许显示带有实际(假)数据和预期结果的简化版本会有所帮助。虽然……你实际问的问题已经回答了?
-
Here's a slightly simplified demo 表明我的意思;第一种情况的所有条件(在内联视图中)都得到满足,但只有第二种情况(在外部查询中)的前两个条件得到满足。