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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:44:27+00:00 2026-05-28T17:44:27+00:00

I have following query working : SELECT COUNT(id), AgeRange FROM ( select id, case

  • 0

I have following query working :

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;

Now my requirement is that, I want these result to be printed always in a sequence, first for less than 0, than for 31-60 and so on.

Even if get count 0 for any interval say 31-60. It should return 0 for that interval. Can anyone help.

Further clarification: what I want is if for any case I am getting zero count it should also print that.

In my table of database for this query it is not displaying the ‘less than 0’ case:

COUNT(ID) AGERANGE       AGERANGESEQUENCE
-------------- -------------- ----------------
     11139 0-30                          2
      2292 31-60                         3
       329 61-90                         4
      1078 91+                           5
       746 NO INFORMATION                6

Its not showing me the first row of count 0.

I want that too, so that I can get a well defined table structure.

  • 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-28T17:44:27+00:00Added an answer on May 28, 2026 at 5:44 pm

    If you want to order by that column – just add an

    ORDER BY AgeRange
    

    after the GROUP BY AgeRange clause:

    SELECT
       COUNT(id), AgeRange
    FROM
       ( ..... ) T
    GROUP BY
        AgeRange
    ORDER BY
        AgeRange
    

    If that sequence doesn’t work out for you, then you’d have to add a second CASE statement to define a AgeRangeSequence to your inner select and then order by that column:

    SELECT
         COUNT(id), AgeRange, AgeRangeSequence
    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 IS NULL THEN 'NO INFORMATION'
                 ELSE 'no category'
              END AS AgeRange,
              CASE
                 WHEN age < 0 THEN 1
                 WHEN age >= 0 AND age <= 30 THEN 2
                 WHEN age >= 31 AND age <= 60 THEN 3
                 WHEN age >= 61 AND age <= 90 THEN 4
                 WHEN age >= 91 THEN 5
                 WHEN age IS NULL THEN 6   
                 ELSE 7
               END AS AgeRangeSequence,
        FROM dbo.queue 
        WHERE [DATE] BETWEEN '01-Apr-2011' AND '05-May-2011'
       ) T
    GROUP BY
        AgeRange, AgeRangeSequence
    ORDER BY
        AgeRangeSequence
    

    PS: in your CASE statement, you should check for WHEN age IS NULL and not use WHEN age = NULL since you cannot compare to NULL using the normal comparison operators!

    • 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) , case when age < 0 then
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: SELECT c.* FROM companies AS c JOIN users AS
I have the following query: SELECT rowid FROM table1 ORDER BY RANDOM() LIMIT 1
I am using the following query: SELECT (SELECT COUNT(*) FROM blah WHERE MATCH(text) AGAINST
I used the following query to find duplicates: SELECT userID, COUNT(userID) AS NumOccurrences FROM
I have the following query: $sqltext = SELECT Score FROM HighScore . WHERE fbID='$_POST[fbID]'
I have the following MySQL query: SELECT SUM(IF(num_content>3,3,num_content)) FROM ( SELECT DATE(FROM_UNIXTIME(content_timestamp)) as dt,

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.