我在这种情况下表达缺少关键字错误

时间:2018-09-28 08:12:09

标签: sql case

在这种情况下,我收到一个缺少关键字的错误。

原因是什么?

SELECT (CASE WHEN (MM.MAKE_DESCRIPTION = 'TOY')
       THEN
          CASE
             WHEN MM.MODEL_DESCRIPTION = 'SCION' THEN MM.MAKE_DESCRIPTION = 'SCION'
             WHEN MM.MODEL_DESCRIPTION <> 'SCION' THEN MM.MAKE_DESCRIPTION
          END
    ELSE MM.MAKE_DESCRIPTION END) AS MAKE
FROM table_make MM where value ='test';

3 个答案:

答案 0 :(得分:1)

在您的一个when子句中,您似乎已将另一个布尔表达式作为THEN输出,因此与其说然后显示“ SCION”,不如说然后显示了布尔表达式,这没有意义。

SELECT (CASE WHEN (MM.MAKE_DESCRIPTION = 'TOY')
   THEN
      CASE
         WHEN MM.MODEL_DESCRIPTION = 'SCION' THEN 'SCION' --edit here
         WHEN MM.MODEL_DESCRIPTION <> 'SCION' THEN MM.MAKE_DESCRIPTION
      END
ELSE MM.MAKE_DESCRIPTION END) AS MAKE

FROM table_make MM where value ='test';

答案 1 :(得分:0)

您的案例陈述遗漏了两分

1。end将在else之后

2。另一个将作为第一个case when

    SELECT CASE WHEN MM.MAKE_DESCRIPTION = 'TOY'
               THEN
                  CASE
                     WHEN MM.MODEL_DESCRIPTION = 'SCION' then
                     'SCION'
                     WHEN MM.MODEL_DESCRIPTION <> 'SCION' THEN MM.MAKE_DESCRIPTION

            ELSE MM.MAKE_DESCRIPTION END
else null end AS MAKE -- this else for 1st case
  FROM table_make MM where value ='test';

答案 2 :(得分:0)

用AND简化它

CASE 
    WHEN MM.MAKE_DESCRIPTION = 'TOY' AND MM.MODEL_DESCRIPTION = 'SCION' 
        THEN 'SCION'
    ELSE MM.MAKE_DESCRIPTION
END