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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:15:55+00:00 2026-06-16T23:15:55+00:00

Have this query: SELECT count(*) as Total, SUM(CASE WHEN gender = 1 then 1

  • 0

Have this query:

SELECT
  count(*) as Total, 
  SUM(CASE WHEN gender = 1 then 1 ELSE 0 END) Male, 
  SUM(CASE WHEN gender = 2 then 1 ELSE 0 END) Female, 
  SUM(CASE WHEN gender = 0 then 1 ELSE 0 END) Unknown, 
  CASE 
    WHEN age>2 AND age<15 THEN '2-15' 
    WHEN age>18 AND age<25 THEN '18-25' 
  END AS var
FROM
  persons 
WHERE
  1=1 
  AND `date` > '2012-01-10'
  AND `date` < '2013-01-07' 
GROUP BY 
  CASE 
    WHEN age>2 AND age<15 THEN '2-15' 
    WHEN age>18 AND age<25 THEN '18-25'
  END

And is resulting this:

Total Male  Female  Unknown var
29     17   12      0       NULL
7      0    7       0       18-25
3      0    3       0       2-15

1st question:
Why is this resulting that NULL ? What could be done to only show results with values?

2nd question:
mysql is ordering my var column with 18-25 before 2-15, migth be because of number 1 cames first then number 2. But the point is order that as numbers, and 2 came first then 18.

Cheers 🙂

  • 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-16T23:15:57+00:00Added an answer on June 16, 2026 at 11:15 pm

    1st answer:
    It is NULL because it does not satisfy any of your CASE conditions for the age. Adding a clause to the WHERE like this should do it:

    WHERE (age > 2 AND age < 15) OR (age > 18 AND age < 25)
    

    2nd answer:
    You are correct, it is ordering them by strings (because that is what they are). Just change the direction of the sort by doing ORDER ASC or ORDER DESC

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

Sidebar

Related Questions

I have this query: SELECT COUNT(*) AS invoice_count, IFNULL(SUM(qa_invoices.invoice_total), 0) AS invoice_total, IFNULL(SUM(qa_invoices.invoice_discount) ,0)
I have this query: SELECT I1 & , & I2 AS Item_set, Round(Sum([T1].Fuzzy_Value)/Count(*),15) AS
I have this PHP code of: $count = select count(fruit) as total, sum(fruit like
I have this query: SELECT gamer_id,COUNT(*) AS sum, SUM(amount) as amount FROM sales_list WHERE
I have this query: SELECT COUNT(articles.id) AS count FROM articles, xml_documents, streams WHERE articles.xml_document_id
Have this query: SELECT HOUR( DATE ) AS hr, COUNT( * ) AS cnt
i have this query: SELECT `completed`.`ID` AS `ID`,`completed`.`level` AS `level`,`completed`.`completed_in` AS `completed_in`, COUNT(1) AS
i have this simple query : SELECT YEAR(P.DateCreated) ,MONTH(P.DateCreated) ,COUNT(*) AS cnt FROM tbl1,
I have a mysql query like this: $topicreplycount = mysql_query('SELECT COUNT(DISTINCT post_msg_id) FROM phpbb_attachments
I have a query like this: SELECT COUNT(*) AS amount FROM daily_individual_tracking WHERE sales

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.