微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

添加CASE语句的结果

如何解决添加CASE语句的结果

ENDCASE声明中缺少:

SELECT
  surname,
  givenname,
  (CASE WHEN surname = 'Smith' THEN 2 ELSE 0 END)  -- < add END
    + (CASE WHEN givenname = 'John' THEN 1 ELSE 0 END)  -- < add END
    AS score
FROM person
WHERE (surname = 'Smith' AND givenname LIKE 'J%')
  OR  (surname LIKE 'S%' AND givenname = 'John')
  OR  (surname = 'John' AND givenname LIKE 'S%')
  OR  (surname LIKE 'J%' AND givenname = 'Smith')
ORDER BY
  score DESC,
  surname ASC,
  givenname ASC;

解决方法

我有以下Oracle
SQL查询。在给定名称和姓氏的情况下,它会以任一顺序查找所有带有一个名字和另一个名字首字母的记录。我创建了该score列,以将可能的匹配项放置在结果集的顶部。

SELECT
  surname,givenname,(CASE WHEN surname = 'Smith' THEN 2 ELSE 0)
    + (CASE WHEN givenname = 'John' THEN 1 ELSE 0)
    AS score
FROM person
WHERE (surname = 'Smith' AND givenname LIKE 'J%')
  OR  (surname LIKE 'S%' AND givenname = 'John')
  OR  (surname = 'John' AND givenname LIKE 'S%')
  OR  (surname LIKE 'J%' AND givenname = 'Smith')
ORDER BY
  score DESC,surname ASC,givenname ASC;

问题是score在的位置给出了错误+。IDE报告:

语法错误,应为:
/
*
|

以及执行报告:

SQL错误:ORA-00905:缺少关键字

我究竟做错了什么?有没有更好的方法来计算分数?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。