【问题标题】:Having "___ in (select distinct ___ from #temp)" in Case statement在 Case 语句中包含“___ in (select distinct ___ from #temp)”
【发布时间】:2010-09-28 11:56:20
【问题描述】:

我正在努力实现这一目标

select
   case
      when Org_CD = '1111' or Org_CD in (select distinct New_Org_CD from #temp) then 'International'
   end as 'Organisation',
count(*)
from #AnotherTempTable
group by
   case
      when Org_CD = '1111' or Org_CD in (select distinct New_Org_CD from #temp) then 'International'
   end 

我收到了这个错误:

列“#AnotherTempTable.Org_Cd”是 在选择列表中无效,因为它 不包含在任何一个 聚合函数或 GROUP BY 子句。

是因为我不能在 case 语句中使用“in”关键字吗?如果是的话,任何已知的解决方法都会很有帮助!

【问题讨论】:

  • 你想在这里做什么?这对我来说真的没有意义......
  • 所有要做的就是提供一个单独的列,其中包含存在于 #temp 或 1111 中的每个 org_cd 的“组织”。
  • 我刚刚用 count() 更新了它。我试图获取与 1111 或其他组织代码匹配的组织的计数()。

标签: tsql case


【解决方案1】:

我会试试这个...

select
    Org_CD, count(*)
from
    #AnotherTempTable A
    JOIN
    (select distinct New_Org_CD from #temp UNION SELECT '1111') T ON A.Org_CD = T.New_Org_CD
group by
   Org_CD

你不能有这样的内联 IN(CASE + 聚合)

如果这样不行,请给出示例数据和输出

【讨论】:

    【解决方案2】:

    我通过使用“Union”的 gbn 解决方案的变体解决了它。谢谢大家。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 2012-08-28
      • 1970-01-01
      相关资源
      最近更新 更多