I am trying to write a postgres query, which is with CASE statement. The query is as follows:
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
The above query is showing syntax error at 'State' = 'State'
Whereas, when I executed the below query, i get the appropriate results:
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
Please let me know what am I doing wrong.
Edit:
The value after THEN clause is dynamic and it may contain 1 or more values (From a multi select box). I want the query to be executed as
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
Which runs exactly fine, but the problem is that CASE is not returning me the required string.