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

  • Home
  • SEARCH
  • 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 3398116
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:36:38+00:00 2026-05-18T04:36:38+00:00

I’m trying to find the best way to split data into groups, for example

  • 0

I’m trying to find the best way to split data into groups, for example Quintiles (groups of 5). I’ll be using this SQL is to bring back data for an SSRS report.

What i’m currently doing is selecting 3 columns into a temporary table called #quintiles (customer ID, customer spend and adding a rownumber).
The data is ordered by customer spend, highest to lowest.

-Edit-

Not too sure if I made it clear, but by ordering the temporary table by customer spend, when I break it down into groups, Q1 are the highest spenders, and so on, with Q5 being my bottom spenders.

Then I am using the following case statement to group them into 5. [tmp] is the alias for my temporary table #quintiles, as this case section is in the second SELECT statement

CASE
WHEN tmp.Row_number <= ((SELECT COUNT (*) FROM #quintiles)/5)
THEN  'Q1'
WHEN tmp.Row_number BETWEEN ((SELECT COUNT (*) FROM #quintiles)/5) AND ((SELECT COUNT (*) FROM #quintiles)*2/5)
THEN 'Q2'
WHEN tmp.Row_number BETWEEN ((SELECT COUNT (*) FROM #quintiles)*2/5) AND ((SELECT COUNT (*) FROM #quintiles)*3/5)
THEN 'Q3'
WHEN tmp.Row_number BETWEEN ((SELECT COUNT (*) FROM #quintiles)*3/5) AND ((SELECT COUNT (*) FROM #quintiles)*4/5)
THEN 'Q4'
WHEN tmp.Row_number > ((SELECT COUNT (*) FROM #quintiles)*4/5)
THEN 'Q5'
END

This works at the moment as it is, and does bring back the customers grouped into groups of 5. I couldn’t just divide the total by 5 as if there is an odd number of customers I would end up with 0.5 customers in each group!

I just don’t feel that this is the best way to have tackled the problem.

I hope it makes sense.

  • 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-18T04:36:38+00:00Added an answer on May 18, 2026 at 4:36 am

    Use NTILE

    SELECT 'Q' + CAST(NTILE(5) OVER (ORDER BY customer_spend) AS VARCHAR(1)) AS Quintile
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.