2013-02-25 61 views
4

我正在檢查很多存在的類似錯誤的問題,但無法找到能幫助我解決錯誤的問題 我選擇了案件在接近'then'的情況下指定的非布爾類型的表達式

select 
    case 
    when e.EthnCode in ('N','A','B','P','W') then ethnicity 
    else 'Multi' 
    end as Ethnicity, 
    case when cat.CategCode IN ('CH','IN','NB','PG','PP','SR')then Category else 0 end as ' ', 
--COUNT (c.EthnCode) as '(A) tottal Numbers of participamts by Rase', 
sum(case when C.StatusID =1 or C.StatusID =2 then 1 else 0 end), 
sum(case when c.Hispanic then 1 else 0 end) as 'Hisp' 
from Ethnicity E 
LEFT JOIN Clients C 
ON c.EthnCode = e.EthnCode 
LEFT join Category cat 
ON c.CategCode = cat.CategCode 
where c.StatusID = 1 
group by case 
    when e.EthnCode in ('N','A','B','P','W') then ethnicity 
    else 'Balance Reporting More Than One Race' 
    end 

,並拋出錯誤

Msg 4145, Level 15, State 1, Line 9 
An expression of non-boolean type specified in a context where a condition is expected, near 'then'. 

9號線

sum(case when c.Hispanic then 1 else 0 end) as 'Hisp' 

它強調西班牙 請需要幫助:)

+1

c.Hispanic是什麼類型?當'c.Hispanic'的情況下應該是'case when c.Hispanic = [something]' – climbage 2013-02-25 23:45:46

回答

3
sum(case when c.Hispanic then 1 else 0 end) as 'Hisp' 

變化(請使用正確的值和數據類型的比較)

sum(case when c.Hispanic = 1 then 1 else 0 end) as 'Hisp' 

或一個簡單的情況下,

sum(case c.Hispanic when 1 then 1 else 0 end) as 'Hisp' 
+0

它引發一個新的錯誤Msg 8120,Level 16,State 1,Line 3 Column'Ethnicity.Ethnicity'在選擇列表,因爲它不包含在聚合函數或GROUP BY子句中。拋出新錯誤 – Andrey 2013-02-25 23:54:30

+0

那麼你的舊錯誤是排序的,這是一個新的錯誤。您需要糾正聚合函數的group by子句。 – Kaf 2013-02-25 23:59:35

1

它不認爲C.Hispanic是一個布爾值。 Case When C.Hispanic = 1 Then ....

或者其他一些會將它整理出來。

相關問題