SQL错误:全文谓词不能出现在聚合表达式中

时间:2015-03-12 05:32:41

标签: sql sql-server database

我正在尝试执行以下查询并收到错误。 但是当我试图使用评论线时它的工作。但不使用包含函数。

select
2015-min(c.Year)+1  as experiance 
, sum (case when not c.JudgementID_FK is null then 1 else 0 end ) as reported_Judgements
, sum( case 
 when 
 (
 Contains( c.Result , 'ACCEPT')
 or 
Contains( c.Result , 'ALLOW' )
or 
contains (c.Result , 'Grant')
    -- (
    -- c.Result = 'Accepted' or c.Result = 'Allowed' 
    -- or c.Result='allowed' or c.Result='accepted'
    -- or c.Result ='bail allowed(accepted)'
    -- or c.Result = 'admitted'
    -- or c.Result = 'Accepted.'
    -- or c.Result = 'accepted.'
    -- ) 
    -- and 
 --cl.LawyerOf = 'Petitioner' 
 )
  then 1 else 0  end) as win_cases

 , sum (case when c.JudgementID_FK is null then 1 else 1 end ) as total_Cases
from CaseTLS c, CaseLawyer cl , Lawyer l 
Where 

 c.CaseId = cl.CaseId
and cl.ComputerCode = l.ComputerCode

group by l.computercode
order by l.computercode

1 个答案:

答案 0 :(得分:0)

根据this documentation" CONTAINS是WHERE子句中使用的谓词"并且您正试图在SELECT子句中使用它。

尝试使用PATINDEX:

select
2015-min(c.Year)+1  as experiance 
, sum (case when not c.JudgementID_FK is null then 1 else 0 end ) as reported_Judgements
, sum( case 
 when 
 (
 PATINDEX( '%ACCEPT%', UPPER(c.Result)) > 0
 or 
 PATINDEX( '%ALLOW%',  UPPER(c.Result)) > 0
 or 
 PATINDEX( '%GRANT%',  UPPER(c.Result)) > 0
 )
  then 1 else 0  end) as win_cases

 , sum (case when c.JudgementID_FK is null then 1 else 1 end ) as total_Cases
from CaseTLS c, CaseLawyer cl , Lawyer l 
Where 

 c.CaseId = cl.CaseId
and cl.ComputerCode = l.ComputerCode

group by l.computercode
order by l.computercode