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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:29:45+00:00 2026-05-21T07:29:45+00:00

I’d like to construct a single query (or as few as possible) to group

  • 0

I’d like to construct a single query (or as few as possible) to group a data set. So given a number of buckets, I’d like to return results based on a specific column.

So given a column called score which is a double which contains:

 90.00
 91.00
 94.00
 96.00
 98.00
 99.00

I’d like to be able to use a GROUP BY clause with a function like:

SELECT MIN(score), MAX(score), SUM(score) FROM table GROUP BY BUCKETS(score, 3)

Ideally this would return 3 rows (grouping the results into 3 buckets with as close to equal count in each group as is possible):

 90.00, 91.00, 181.00
 94.00, 96.00, 190.00
 98.00, 99.00, 197.00

Is there some function that would do this? I’d like to avoid returning all the rows and figuring out the bucket segments myself.

Dave

  • 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-21T07:29:46+00:00Added an answer on May 21, 2026 at 7:29 am
    create table test (
    id int not null auto_increment primary key,
    val decimal(4,2)
    ) engine = myisam;
    
    insert into test (val) values 
     (90.00),
     (91.00),
     (94.00),
     (96.00),
     (98.00),
     (99.00);
    
    select min(val) as lower,max(val) as higher,sum(val) as total from (
    select id,val,@row:=@row+1 as row
    from test,(select @row:=0) as r order by id
    ) as t
    group by ceil(row/2)
    
    +-------+--------+--------+
    | lower | higher | total  |
    +-------+--------+--------+
    | 90.00 |  91.00 | 181.00 |
    | 94.00 |  96.00 | 190.00 |
    | 98.00 |  99.00 | 197.00 |
    +-------+--------+--------+
    3 rows in set (0.00 sec)
    

    Unluckily mysql doesn’t have analytical function like rownum(), so you have to use some variable to emulate it. Once you do it, you can simply use ceil() function in order to group every tot rows as you like. Hope that it helps despite my english.

    set @r = (select count(*) from test);
    select min(val) as lower,max(val) as higher,sum(val) as total from (
    select id,val,@row:=@row+1 as row
    from test,(select @row:=0) as r order by id
    ) as t
    group by ceil(row/ceil(@r/3))
    

    or, with a single query

    select min(val) as lower,max(val) as higher,sum(val) as total from (
    select id,val,@row:=@row+1 as row,tot
    from test,(select count(*) as tot from test) as t2,(select @row:=0) as r order by id
    ) as t
    group by ceil(row/ceil(tot/3))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JSP page retrieving data and when single or double quotes are
I have some data like this: 1 2 3 4 5 9 2 6
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I have text I am displaying in SIlverlight that is coming from a CMS
I want to count how many characters a certain string has in PHP, but

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.