2

我有以下 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:缺少关键字

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

4

1 回答 1

6

您的陈述中缺少以下END内容:CASE

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;
于 2012-12-03T01:32:03.690 回答