【问题标题】:An expression of non-boolean type specified in a context where a condition is expected, near 'then'在预期条件的上下文中指定的非布尔类型的表达式,靠近 'then'
【发布时间】:2013-02-11 06:56:38
【问题描述】:

我正在检查很多存在类似错误的问题,但找不到可以帮助我解决错误的答案 我已经选择了案例

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'

强调西班牙裔 请需要帮助:)

【问题讨论】:

  • c.西班牙裔是什么类型?表达式case when c.Hispanic 应该是case when c.Hispanic = [something]

标签: sql sql-server select sql-server-2008-r2


【解决方案1】:

它不认为 C.Hispani 是布尔值。 Case When C.Hispanic = 1 Then ....

或者一些这样的人会解决它。

【讨论】:

    【解决方案2】:
     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'
    

    【讨论】:

    • 它抛出一个新错误 Msg 8120, Level 16, State 1, Line 3 列 'Ethnicity.Ethnicity' 在选择列表中无效,因为它不包含在聚合函数或 GROUP BY 中子句.it trowing 新错误
    • 您的旧错误已排序,这是一个新错误。您需要更正聚合函数的 group by 子句。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-05
    • 2015-10-23
    • 1970-01-01
    • 2011-09-22
    • 1970-01-01
    相关资源
    最近更新 更多