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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:19:06+00:00 2026-06-01T23:19:06+00:00

Now here’s a fun MySQL question, I wonder if it’s even possible! Disclaimer: Although

  • 0

Now here’s a fun MySQL question, I wonder if it’s even possible!

Disclaimer: Although it’s very similar question that I asked before, it actually is COMPLETELY different. Just saying before anyone says I’ve asked this before.

For this example lets say I want SUMS() of multiples of 20.

I want to SUM() the row score and return the date.

Lets say I have the following table sorted by date ASC:

Data

score |   date
  4     2000-01-01
  2     2000-01-02
  6     2000-01-03
  1     2000-01-04 //Score 4+2+6+1 = 13
  7     2000-01-05 //Score 4+2+6+1+7 = 20 so return this date
  1     2000-01-06
  2     2000-01-07
  1     2000-01-08
  5     2000-01-09
  1     2000-01-10
  9     2000-01-11 //Score = 39 so far.
  7     2000-01-12 //Score = 46 It's not 40 but is the closest number above 40 so return it.
  3     2000-01-13
  4     2000-01-14
  7     2000-01-15 //Score = 60, return this date.

Expected results:

score | date
 20     2000-01-05
 40     2000-01-12
 60     2000-01-15

And etcetera. Is it possible to do this in MySQL?

  • 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-01T23:19:07+00:00Added an answer on June 1, 2026 at 11:19 pm

    By using SQL Variables, you don’t have to keep doing recursive aggregations for every subsequent row to tally up to the given entity. This does each one in sequence with a flag of which one triggers the multiple of 20. That result is then processed out only where the “ThisOne” flag is set to 1.

    select 
          M20.*
       from 
          ( select 
                  TransDate,
                  score,
                  if( @runTotal + Score >= 20 * @multCnt, 1, 0 ) as ThisOne,
                  @multCnt := @multCnt + if( @runTotal + Score >= 20 * @multCnt, 1, 0 ) as nextSeq,
                  @runTotal := @runTotal + Score
               from Mult20s,
                    ( select @multCnt := 1,
                             @runTotal := 0 ) sqlvars
               order by transdate ) M20
       where
          M20.ThisOne = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I asked the question here and now i would like to reverse this procedure
Variants of this question have been asked several times now here, but my question
This question follows another, just solved here Now I want to do a different
Now I must be missing something here, as this seems a very basic issue
Now here's a fun problem. I have an object array as the following: objRequests
I use mobile detection from http://detectmobilebrowsers.mobi/ and that is working ok. Now here is
now here is a problem with me that how can I enter the current
A php student here that has been stuck for many hours now :( Here's
i have this problem that i can't solve for days now...here is my code.
I got a simple question here Let's suppose we have a codebase that employs

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.