2013-02-25 28 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 ....

或者其他一些会将它整理出来。

相关问题