使用case语句时,在“case”或其附近的语法错误

时间:2021-09-22 22:48:51

I have the following SQL query (in PostgreSQL) and I keep getting a syntax error.

我有以下SQL查询(在PostgreSQL中),我不断收到语法错误。

SELECT data1, data1_class
CASE
    WHEN data1 LIKE '%Bookmarked Removed%' THEN 'Class I'
    ELSE 'Class II'
END
AS data1_class
from events
WHERE event_code = 11

The syntax error I get is:

我得到的语法错误是:

  ERROR:  syntax error at or near "CASE"
  LINE 2:     CASE
        ^
  ********** Error **********

  ERROR: syntax error at or near "CASE"
  SQL state: 42601
  Character: 31

To me, I don't think that there is an error and this should work however after trying to work it out a few times I can't it to work.

对我来说,我认为没有错误,但这应该有效,但是在尝试了几次之后我就无法工作了。

What am I missing from my CASE statement to make it work?

我的CASE声明中缺少什么让它起作用?

2 个解决方案

#1


1  

You are missing a comma before the CASE statement. It should be e.g.

您在CASE语句之前缺少逗号。它应该是例如

SELECT data1, data1_class,
CASE
 ...

#2


0  

You are missing a comma before the CASE statement. It should be e.g.

您在CASE语句之前缺少逗号。它应该是例如

SELECT data1, data1_class,
CASE
 ...

Though you repeat the data1_class as an alias for your case statement, so perhaps you mean just:

虽然你重复data1_class作为case语句的别名,所以也许你的意思是:

SELECT data1,
CASE
  ...

#1


1  

You are missing a comma before the CASE statement. It should be e.g.

您在CASE语句之前缺少逗号。它应该是例如

SELECT data1, data1_class,
CASE
 ...

#2


0  

You are missing a comma before the CASE statement. It should be e.g.

您在CASE语句之前缺少逗号。它应该是例如

SELECT data1, data1_class,
CASE
 ...

Though you repeat the data1_class as an alias for your case statement, so perhaps you mean just:

虽然你重复data1_class作为case语句的别名,所以也许你的意思是:

SELECT data1,
CASE
  ...