每当我尝试这段代码时,我都会遇到这个令人讨厌的错误:
select
gam.acid,
gam.foracid,
gam.acct_name,
gam.sol_id,
sol.sol_desc,
gam.gl_sub_head_code,
case gsh.gl_code
when cast(gl_code as int) >= 01 and cast(gl_code as int) <=13
or cast(gl_code as int) >= 15 and cast(gl_code as int) <=24 then 'A'
when cast(gl_code as int) >= 28 and cast(gl_code as int) <= 31
or cast(gl_code as int) >= 33 and cast(gl_code as int) <= 39
or cast(gl_code as int) = 41
or cast(gl_code as int) >= 43 and cast(gl_code as int) <= 48 then 'L'
when cast(gl_code as int) = 51
or cast(gl_code as int) = 53
or cast(gl_code as int) >= 55 and cast(gl_code as int) <= 57 then 'C'
when cast(gl_code as int) >= 61 and cast(gl_code as int) <= 70
or cast(gl_code as int) >= 72 and cast(gl_code as int) <= 81 then 'I'
when cast(gl_code as int) = 84 or cast(gl_code as int) = 85
or cast(gl_code as int) >= 87 and cast(gl_code as int) <= 94 then 'E'
when cast(gl_code as int) >= 97 and cast(gl_code as int) <= 99 then 'S'
else null
end as gl_classification
from tbaadm.gam
我觉得代码中有些东西不合适,或者我忘了做/添加一些东西。任何帮助将不胜感激。
答案 0 :(得分:3)
CASE
construct有两种替代语法:
CASE foo WHEN 1 THEN 'blah' END
CASE WHEN foo=1 THEN 'blah' END
你试图同时使用两者:
case gsh.gl_code
when cast(gl_code as int) >= 01
应该是:
select case
--gsh.gl_code remove this
when cast(gl_code as int) >= 01 and cast(gl_code as int) <=13
or cast(gl_code as int) >= 15 and cast(gl_code as int) <=24 then 'A'
when cast(gl_code as int) >= 28 and cast(gl_code as int) <= 31
or cast(gl_code as int) >= 33 and cast(gl_code as int) <= 39
or cast(gl_code as int) = 41
or cast(gl_code as int) >= 43 and cast(gl_code as int) <= 48 then 'L'
when cast(gl_code as int) = 51
or cast(gl_code as int) = 53
or cast(gl_code as int) >= 55 and cast(gl_code as int) <= 57 then 'C'
when cast(gl_code as int) >= 61 and cast(gl_code as int) <= 70
or cast(gl_code as int) >= 72 and cast(gl_code as int) <= 81 then 'I'
when cast(gl_code as int) = 84 or cast(gl_code as int) = 85
or cast(gl_code as int) >= 87 and cast(gl_code as int) <= 94 then 'E'
when cast(gl_code as int) >= 97 and cast(gl_code as int) <= 99 then 'S'
else null
end as gl_classification
from tbaadm.gam
答案 1 :(得分:1)
您可以尝试使用括号来优先考虑AND / OR条件。例如:
when (cast(gl_code as int) >= 01 and cast(gl_code as int) <=13)
or (cast(gl_code as int) >= 15 and cast(gl_code as int) <=24) then 'A'
它可能有帮助...