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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:49:08+00:00 2026-06-04T01:49:08+00:00

Here is my code SELECT CASE WHEN Money >= 20000 THEN ‘$ 20,000 +

  • 0

Here is my code

SELECT
  CASE
       WHEN Money >= 20000 THEN '$ 20,000 + '
       WHEN Money BETWEEN 10000 AND 19999 THEN '$ 10,000 - $ 19,999'
       WHEN Money BETWEEN  5000 AND  9999 THEN '$  5,000 - $  9,999'
       WHEN Money BETWEEN     1 AND  4999 THEN '$      1 - $  4,999'
       ELSE '$      0'
   END AS [MONEY],
       COUNT(*) AS [#],
       MAX(Money) AS [MAX]
  FROM MyTable
 WHERE MoneyType = 'Type A'
 GROUP BY 
  CASE
       WHEN Money >= 20000 THEN '$ 20,000 + '
       WHEN Money BETWEEN 10000 AND 19999 THEN '$ 10,000 - $ 19,999'
       WHEN Money BETWEEN  5000 AND  9999 THEN '$  5,000 - $  9,999'
       WHEN Money BETWEEN     1 AND  4999 THEN '$      1 - $  4,999'
       ELSE '$      0'
   END
 ORDER BY MAX DESC 

Now my issue is I want all the cases to show a row in my result set but, since I dont have any values that would fall between 1 AND 4999 that row doesn’t show up. I would still like that row to show up and just contain 0’s for it’s columns (except the first of course). Anyone can show me how to modify the code to accomplish this? Perhaps I need to do it a different way… Thanks!

Example of result set I’m looking for …

  |  [MONEY]              |   [#]   |    [MAX]    |
  |  $ 20,000+            |   2     |    30,000   |
  |  $ 10,000 - $ 19,999  |   8     |    19,000   |
  |  $  5,000 - $  9,999  |   4     |     8,000   |
  |  $      1 - $  4,999  |   0     |     0       |     <-- Row currently doesn't show
  |  $      0             |   12    |     0       |
  • 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-04T01:49:09+00:00Added an answer on June 4, 2026 at 1:49 am

    You could build up a lookup table with a CTE and then use that to group on that instead of the Case statement. You’ll need to make three other changes

    • The COUNT will need to be COUNT(t.Money) or you’ll get a 1 when you’re expecting a ZERO.
    • You may want to COALESCE your MAX(Money) but I’m not sure what you want it to be when its NULL
    • You can’t actually order by MAX(MONEY) because it may be null. So its better to use the CTE to control the order as well

    WITH Ranges AS
    ( SELECT 1 id , '$ 20,000 +'  description
      UNION SELECT 2 , '$ 10,000 - $19,999'
      UNION SELECT 3, '$  5,000 - $ 9,999'
      UNION SELECT 4, '$      1 - $ 4,999'
      UNION SELECT 5, '$      0')
    
    
    SELECT
           r.Description as money,
           COUNT(t.Money) AS [#],
           MAX(Money) AS [MAX]
      FROM 
           Ranges r
           LEFT JOIN
            MyTable t
           ON r.ID =   CASE
                        WHEN Money >= 20000 THEN 1
                        WHEN Money BETWEEN 10000 AND 19999 THEN 2
                        WHEN Money BETWEEN  5000 AND  9999 THEN 3
                        WHEN Money BETWEEN     1 AND  4999 THEN 4
                        ELSE 5
                     END 
            AND  MoneyType = 'Type A' 
     GROUP BY 
      r.id,
      r.Description
     ORDER BY r.id asc
    

    LIVE DEMO

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

Sidebar

Related Questions

Here's my code: select yr,count(*) from movie join casting on casting.movieid=movie.id join actor on
hi i am trying to get nearest Date here is my code SELECT schedule_date
Here is the pseudo-code for my inline query in my code: select columnOne from
i have two dropdown list. first drop down:1 enter code here <form:select path=custName id=custName>
here is the code private ResultSet rsResult; try { rsResult = DBProcess.statement.executeQuery(SELECT * FROM
i have this code right here: <select name=group> <option value=>Choose a group....</option> <?php foreach($groups
I use GetOpenFilename() to let the user select a file. Here is the code:
Ok, so here is the code that I'm trying to execute. begin transaction; SELECT
firstly, here's my code SELECT dbo.tblPat.pID, dbo.tblRec.rID, right(rPCheck, LEN(rPCheck) - (CHARINDEX('|', rPCheck))) as pCheck,
I'm trying to do the equivalent of this SQL Code SELECT ID SUM(CASE WHEN

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.