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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:47:42+00:00 2026-05-27T18:47:42+00:00

I often find when I am pulling data for analysis, that I group the

  • 0

I often find when I am pulling data for analysis, that I group the number of orders a customer has placed into ranges, such as:

1-2
3-5
6-9
10-12
13-15

I do this with a CASE function. However, when you get the query results, the order ranges will be listed like:

1-2
10-12
13-15
3-5
6-9

This easy to correct in Excel when you have 1 query and a few order range groups. However, when you’re pulling many queries, it’s a pain to correct this over and over.

What is the best way to pull a range and have it ordered correctly?

here’s an example of the query I would write:

SELECT 
    OrderRange = CASE
        WHEN COUNT(OrderID) BETWEEN 1 AND 5 THEN '1-5'
        WHEN COUNT(OrderID) BETWEEN 6 AND 10 THEN '6-10'
        WHEN COUNT(OrderID) > 10 THEN '10+'
        ELSE 'Error'
    END
FROM Orders
GROUP BY CASE
    WHEN COUNT(OrderID) BETWEEN 1 AND 5 THEN '1-5'
    WHEN COUNT(OrderID) BETWEEN 6 AND 10 THEN '6-10'
    WHEN COUNT(OrderID) > 10 THEN '10+'
    ELSE 'Error'
    END
ORDER BY...  ?
  • 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-27T18:47:42+00:00Added an answer on May 27, 2026 at 6:47 pm

    I’d keep a table of ranges, e.g. (indices not written)

    CREATE TABLE Ranges (RangeSet int, MinVal int, MaxVal int, Name varchar(50));
    

    and then e.g.

    INSERT INTO ranges VALUES
      (1,1,5,'1-5'),(1,6,10,'6-10'),(1,11,-1,'11+'),
      (2,1,10,'1-10'),(2,11,20,'11-20'),(2,21,30,'21-30'),(2,31,-1,'31+');
    

    you get the idea. Now you do something like (table and field names free fiction)

    SELECT
      CustomerID,
      count(OrderID) AS OrderCount
    FROM Orders
    WHERE <whatever, e.g order_date BETWEEN ... AND ...>
    GROUP BY CustomerID
    HAVING OrderCount>0
    

    as you’d normally would expect, but wrap it in a superquery joining to the Ranges table

    SELECT
      BaseView.CustomerID as CustomerID,
      Ranges.Name as OrderRange
    FROM (
      SELECT
        CustomerID,
        count(OrderID) AS OrderCount
      FROM Orders
      WHERE <whatever, e.g order_date BETWEEN ... AND ...>
      GROUP BY CustomerID
      HAVING OrderCount>0
    ) AS BaseView
    INNER JOIN Ranges ON
      Ranges.RangeSet=<id-of-required-rangeset>
      AND BaseView.OrderCount>=Ranges.MinVal
      AND (BaseView.OrderCount<=Ranges.MaxVal OR Ranges.MaxVal=-1)
    ORDER BY RangeSet.MinVal DESC
    ;
    

    Now you just have to supply the RangeSet you want to apply, maybe creating a new one on occasion.

    Disclaimer: This is a performance-killer

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

Sidebar

Related Questions

I often find myself adding expressions in the group by clause that I am
I often find myself in the following situation: The customer reports corrupt data in
I often find myself writing a property that is evaluated lazily. Something like: if
I often find myself wanting to debug CSS layout issues that involve DOM changes
I often find myself needing reference to an object that is several objects away,
I often find that I want an element to adjust its width to the
I often find myself doing inefficient loops as such: def __add__(self, other): dimensions =
I often find myself remembering the name of a class that I want to
I often find that in writing tests for a method I want to throw
I often find myself wanting to scroll a Vim window so that the line

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.