我正在尝试编写一个带有 CASE 语句的 postgres 查询。查询如下:
SELECT "State" AS x, count("Age Group") AS y from test.smp_state_survey where "State" IN
(CASE
WHEN 'State' = 'State' THEN 'State 1','State 2','State 3'
WHEN 'State' = 'District' THEN 'State 1'
END)
group by x ORDER BY x,y
上面的查询显示语法错误'State' = 'State'
然而,当我执行以下查询时,我得到了适当的结果:
SELECT "State" AS x, count("Age Group") AS y from test.smp_state_survey where "State" IN
(CASE
WHEN 'State' = 'State' THEN 'State 1'
WHEN 'State' = 'District' THEN 'State 1'
END)
group by x ORDER BY x,y
请让我知道我做错了什么。
编辑:
THEN 子句之后的值是动态的,它可能包含 1 个或多个值(来自多选框)。我希望查询被执行为
SELECT "State" AS x, count("Age Group") AS y from test.smp_state_survey where "State" IN
('State 1','State 2','State 3')
group by x ORDER BY x,y
运行得很好,但问题是 CASE 没有返回我所需的字符串。