case 操作数类型不匹配 when 子句操作数类型 integer vs boolean athena

case operand type does not match when clause operand type integer vs boolean athena

我正在尝试使用 sql 在 aws athena 中的整数字段上使用 case 语句,但我收到错误消息,指出 case 操作数类型与 when 子句操作数类型整数与布尔值不匹配。我已经验证字段类型是整数。附上下面的代码。

SELECT a.open_dt, 
Case a.terms_duration when a.terms_duration<=6 then '<=6'
    when a.terms_duration>6 and a.terms_duration<=12 then '6-12'
    when a.terms_duration>12 and a.terms_duration<=18 then '12-18'
    when a.terms_duration>18 and a.terms_duration<=36 then '18-36'
    when a.terms_duration>36 and a.terms_duration<=48 then '36-48'
    when a.terms_duration>48 and a.terms_duration<=60 then '48-60'
    when a.terms_duration>60 then '60+'
    Else 'Missing' END
    FROM sx06grp.sx06am_pmnt_sbfe_crdb_sampleno_1_hp a limit 10;

这是错误

An error occurred when executing the SQL command:
SELECT a.open_dt, 
Case a.terms_duration when a.terms_duration<=6 then '<=6'
    when a.terms_duration>6 and a.terms_duration<=12 then '6-12'
    when...

[Simba][AthenaJDBC](100071) An error has been thrown from the AWS Athena client. SYNTAX_ERROR: line 2:23: CASE operand type does not match WHEN clause operand type: integer vs boolean [SQL State=HY000, DB Errorcode=100071]
1 statement failed.

Execution time: 0.6s

每个 when 子句后都有布尔表达式,而 case 子句后的表达式类型不匹配。

事实上你不需要在 case 子句后面的表达式来满足你当前案例的目的,然后就去掉它,比如

CASE 
  WHEN a.terms_duration <= 6 THEN
   '<=6'
  WHEN a.terms_duration >  6 AND a.terms_duration <= 12 THEN
   '6-12'
  WHEN a.terms_duration > 12 AND a.terms_duration <= 18 THEN
   '12-18'
  WHEN a.terms_duration > 18 AND a.terms_duration <= 36 THEN
   '18-36'
  WHEN a.terms_duration > 36 AND a.terms_duration <= 48 THEN
   '36-48'
  WHEN a.terms_duration > 48 AND a.terms_duration <= 60 THEN
   '48-60'
  WHEN a.terms_duration > 60 THEN
   '60+'
  ELSE
   'Missing'
 END