1

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.

4 Answers 4

5

Your query should be like this, i hope this will work for you

SELECT  "State" AS x, count("Age Group") AS y from test.smp_state_survey where  
CASE  
        WHEN 'State' = 'State'  THEN  "State" IN  ('State 1' ,'State 3')
        WHEN 'State' = 'District' THEN "State" IN  ('District 1')
    END
  group by x ORDER BY x,y 
Sign up to request clarification or add additional context in comments.

Comments

2

'State' = 'State' is a tautology. Do you mean "State" = 'State', i.e. the value of the column "State" equal to the string 'State'?

Comments

1

Since you're trying to use IN, you can make use of the fact that IN accepts either a set or an array:

SELECT "State" AS x, count("Age Group") AS y
FROM test.smp_state_survey
WHERE "State" IN (CASE
  WHEN 'State' = 'State' THEN ARRAY['State 1','State 2','State 3']
  WHEN 'State' = 'District' THEN ARRAY['State 1']
END) 
GROUP BY x
ORDER BY x,y 

or use @user1327246's rephrasing where you push the IN test into the CASE statement.

Comments

0

You cant have multiple values for a single case.

Thats the issue

WHEN 'State' = 'State' THEN 'State 1','State 2','State 3'

4 Comments

What is exactly your objective when state=state what should the value be ?
I am not OP. Ankit is an OP. But looking at his condition, he wants to return all three values (state1,state2,state3) when 'State' = 'State'
then that should be a single string like 'State 1 State 2 State 3'
@shamis: I want the output of CASE to be as input of IN so that the query is like -- where "State" IN ('State 1','State 2','State 3')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.