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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:58:39+00:00 2026-05-16T10:58:39+00:00

I’m looking to create a histogram in SQL (which in itself isn’t too tricky),

  • 0

I’m looking to create a histogram in SQL (which in itself isn’t too tricky), but what I’m looking for is a way of splitting the bins so that each bin / band has the same proportion of the data included within.

For example if I have the sample data (the value column) and I want to divide it into 5 bins, I know that I can work out the number of bins by doing something like

(MAX(Value) - MIN(Value)) / numberofsteps

Will give the groups we see in the band 1 column.

However what I want is for the bands to be calculated so that each band accounts for (100 / n) % of the total where n is the number of bands (so in this case each of the 5 bands would represent 20% of the total data) – which is what is shown in the band 2 column

Value      band 1     band 2
1     | 1 to 2    | 0 to 1
1     | 1 to 2    | 0 to 1
1     | 1 to 2    | 0 to 1
1     | 1 to 2    | 0 to 1
2     | 1 to 2    | 2 to 3
2     | 1 to 2    | 2 to 3
3     | 1 to 2    | 2 to 3
3     | 1 to 2    | 2 to 3
4     | 3 to 4    | 4 to 6
4     | 3 to 4    | 4 to 6
5     | 5 to 6    | 4 to 6
6     | 5 to 6    | 4 to 6
7     | 7 to 8    | 7 to 8
8     | 7 to 8    | 7 to 8
8     | 7 to 8    | 7 to 8
8     | 7 to 8    | 7 to 8
9     | 9 to 10   | 9 to 10
10    | 9 to 10   | 9 to 10
10    | 9 to 10   | 9 to 10
10  |    9 to 10   | 9 to 10

Is there a way to do this in SQL (i’m using SQL server 2005 if that helps), possibly without creating a UDF and having it so that I can easily alter the number of bins would be great (if that’s not asking the impossible!)

Thanks

  • 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-16T10:58:40+00:00Added an answer on May 16, 2026 at 10:58 am

    To divide into bins you can use the ntile function.

    with Vals AS
    (
    SELECT 1 AS value UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 8 UNION ALL SELECT 8 UNION ALL SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 10 UNION ALL SELECT 10
    ), TiledVals AS
    (
    SELECT value, NTILE(5) OVER (ORDER BY value) AS BinNumber
    FROM Vals
    )
    SELECT value, BinNumber, 
    Min(value) OVER (PARTITION BY BinNumber) As StartBin,
     MAX(value) OVER (PARTITION BY BinNumber) As EndBin
    FROM TiledVals
    

    Gives

    value       BinNumber            StartBin    EndBin
    ----------- -------------------- ----------- -----------
    1           1                    1           1
    1           1                    1           1
    1           1                    1           1
    1           1                    1           1
    2           2                    2           3
    2           2                    2           3
    3           2                    2           3
    3           2                    2           3
    4           3                    4           6
    4           3                    4           6
    5           3                    4           6
    6           3                    4           6
    7           4                    7           8
    8           4                    7           8
    8           4                    7           8
    8           4                    7           8
    9           5                    9           10
    10          5                    9           10
    10          5                    9           10
    10          5                    9           10
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has

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.