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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:28:05+00:00 2026-05-24T18:28:05+00:00

I have a User table similar to this: | ID | DONATION | RANK

  • 0

I have a User table similar to this:

|  ID  |  DONATION  |   RANK   |
|  4   |        10  |     12   |
|  9   |        20  |      8   |
|  2   |        40  |      5   |
|  3   |        80  |      2   |

I would like to construct a query where I retrieve the number of users where the combined sum of the donations is less than say 100, ordered by rank… something like (obviously not correct):

select count(*) where sum(donation) < 100 order by rank

Can’t figure out how to do this if it’s even possible. Not even sure I’m going about this the right way. Any ideas or pointers?

  • 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-24T18:28:07+00:00Added an answer on May 24, 2026 at 6:28 pm

    Assuming you have the following Donation table (as much as I think your ID column points to a user, I could not be sure):

    | ID | UserId | Donation |
    | 1  | 1      | 60       |
    | 2  | 1      | 35       |
    | 3  | 2      | 70       |
    | 4  | 2      | 40       |
    | 5  | 3      | 90       |
    

    The the following query will give you the number of users with their total donation < 100

    SELECT COUNT(1)
    FROM (
        SELECT  UserId
        FROM    Donations 
        GROUP BY UserId
        HAVING SUM(Donation) < 100
    ) AS t
    

    Update based on extra information.
    I do not believe you can get what you want from a single SQL statement, what you would need to do is create a cursor that loops around the records you want (in appropiate order) inserting the id into a temp table, and maintaining a total of the donations. When the total is above your limit, break out of the cursor loop and return the results. Something like this:

    CREATE PROCEDURE GetTopDontations( limit DECIMAL(...) )
    BEGIN
        CREATE TEMPORARY TABLE t1 (
            id INT
        );
    
        DECLARE total, Donation DECIMAL(...);
        DECLARE Id INT;
        DECLARE curDonations CURSOR FOR SELECT ID, Dontaion FROM Donations ORDER BY <something relevant>;
    
        SET total = 0.0;
        OPEN curDonations;
        read_loop: LOOP
            FETCH curDonations INTO Id, Donation;
            INSERT INTO t1( id ) VALUES ( Id );
            SET total = total + Donation;
            IF( total > limit ) THEN
                LEAVE read_loop;
            END IF;
        END LOOP:
        CLOSE curDonations;
    
        SELECT d.*
        FROM    Donations d
            INNER JOIN t1
                ON d.Id = t1.Id;
    END;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a user table 'users' that has fields like: id first_name last_name ...
I have a user table like this FIRSTNAME | LASTNAME | ID | --------------------------------
I have a table named PAYMENT . Within this table I have a user
I have a reward_points table with structure similar to 'id', 'user_id', 'points', 'datetime_gained'. Each
I have a user table in my mysql database that has a password column.
I have a User table that has all of their avatars saved in an
I have a User table which has a PrivilegeId foreign key points to a
Consider i have a user table and I have three columns mobilePhone , homePhone
Currently I have a User table that has a toys_owned column that is an
I have user and user_role tables. The user_role table has two columns: user_id role_name

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.