2010-09-24 41 views
0

我試圖做到這一點有「___中(選擇不同___從#TEMP)」萬一聲明

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 

我收到此錯誤:

Column '#AnotherTempTable.Org_Cd' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

是不是因爲我不能在case語句中使用「in」關鍵字?如果是的話,任何已知的解決方法都會更有幫助!

+0

你想在這裏做什麼?這對我來說並沒有什麼意義...... – 2010-09-24 00:49:15

+0

所有要做的事情是提供一個列,其中包含每個org_cd的「組織」,它存在於#temp或1111中。 – 2010-09-24 00:54:06

+0

我剛更新了它計數(*)。我試圖爲那些匹配1111或其他組織代碼的組織計數(*)。 – Jason 2010-09-24 03:23:20

回答

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 

你不能有一個這樣的內聯(CASE +骨料)

如果這也不行,請給樣本數據和輸出

0

我用'Union'的gbn解決方案的變體解決了它。謝謝大家。