Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8814687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:11:30+00:00 2026-06-14T04:11:30+00:00

I am trying to write a postgres query, which is with CASE statement. The

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:11:31+00:00Added an answer on June 14, 2026 at 4:11 am

    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 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Postgres and I'm trying to write a query like this: select count(*)
I'm trying write a query to find records which don't have a matching record
I am trying to write a query which loops through a database starting at
Trying to write a javascript bookmarklet to jump from a front end link into
I've had problem when trying write a function which has a default value when
I'm trying write a simple perl script that reads some fields from a password
Trying to write a python application that downloads images from an RSS feed, and
I've been trying write an application which will be able to connect to a
I'm trying to write two types of stored procedures in PostgreSQL. From what I
I am trying to move from a mysql set to a postgres one, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.