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 7175853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:23:18+00:00 2026-05-28T16:23:18+00:00

i have following query working.. select count(id) , case when age < 0 then

  • 0

i have following query working..

select count(id) ,
case
when age < 0 then 'less than 0'
when age >= 0 and age <=30 then '0-30'
when age >= 31 and age <=60 then '31-60'
when age >= 61 and age <=90 then '61-90'
when age >= 91 then '91+'
when age = null then 'NO INFORMATION'
else 'no catagory'
end
from queue where DATE between '01-Apr-2011' and '05-May-2011'
group by case
when age < 0 then 'less than 0'
when age >= 0 and age <=30 then '0-30'
when age >= 31 and age <=60 then '31-60'
when age >= 61 and age <=90 then '61-90'
when age >= 91 then '91+'
when age = null then 'NO INFORMATION'
else 'no catagory'
end
/

now my requirement is that, i dont want to use CASE clause again for grouping it with count..
by using CASE clause only once i want the same result..

also i want a sequence always to be followed by result of this query..
for ex it always print count first for ‘less than 0’ then for ‘0-30’ and so on.. even if there is 0 count found for interval say 31-60 it should print 0 for that it interval…

can anyone plzz help..
thanx in advance

  • 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-05-28T16:23:19+00:00Added an answer on May 28, 2026 at 4:23 pm

    You have to group by on all non-aggregated, non-constant columns and expressions
    So you either repeat the entire CASE or you use a derived table like this if you don’t
    want to repeat it.

    This is standard SQL, should work in all mainstream RDBMS

    SELECT
      COUNT(id), AgeRange
    FROM
      (
      select
        id,
        case
        when age < 0 then 'less than 0'
        when age >= 0 and age <=30 then '0-30'
        when age >= 31 and age <=60 then '31-60'
        when age >= 61 and age <=90 then '61-90'
        when age >= 91 then '91+'
        when age = null then 'NO INFORMATION'
        else 'no catagory'
        end AS AgeRange
      from queue 
      where DATE between '01-Apr-2011' and '05-May-2011'
      ) T
    GROUP BY
      AgeRange;
    

    Edit: Note I aliased the CASE as AgeRange and grouped on that.

    GROUP BY (ordinal) isn’t standard SQL. It doesn’t work in SQL Server

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following query working : SELECT COUNT(id), AgeRange FROM ( select id, case
I have the following query: select column_name, count(column_name) from table group by column_name having
I have the following query: SELECT created_at::DATE, count (*) FROM messages WHERE city =
I have the following SQL query: select expr1, operator, expr2, count(*) as c from
I have the following query which is working fine, but I also want to
I have the following xpath query which seems to be working but I just
I have the following query: SELECT c.* FROM companies AS c JOIN users AS
I have the following query: SELECT rowid FROM table1 ORDER BY RANDOM() LIMIT 1
I have the following query and it's not working exactly as i want it
I am using the following query: SELECT (SELECT COUNT(*) FROM blah WHERE MATCH(text) AGAINST

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.