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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:15:42+00:00 2026-05-27T19:15:42+00:00

How is it possible to return rows with an accumulate sum for a row

  • 0

How is it possible to return rows with an accumulate sum for a row bigger or smaller than a specified value?

table:

id | count
-----------
1 | 30
2 | 10
3 | 5
4 | 20
5 | 15

query:

SELECT id, count
FROM table
ORDER BY id
HAVING SUM(count) < 50

return rows:

id | count
-------------
1 | 30
2 | 10
3 | 5

update

code:

public function query(){
    switch($this->table){
        case 'in_stock':
            return "SELECT * FROM ".Init::$static['db'].".stock
                WHERE id<=dynaccount.stock_first_available_id(".$this->value['num_stock'].", ".$this->value['product_id'].", ".(isset($this->att_arr['gid']) ? $this->att_arr['gid']:$_SESSION['gid']).")
                ORDER BY time, id";
    }
}

procedure:

DELIMITER $$

DROP FUNCTION IF EXISTS `stock_first_available_id` $$
CREATE DEFINER=`dynaccount`@`localhost` FUNCTION `stock_first_available_id`(_running_total_limit INT, _product_id INT, _group_id INT) RETURNS INT
BEGIN
    DECLARE _running_count INT default 0;
    DECLARE _id INT;
    DECLARE _current_id INT;
    DECLARE _sum_count INT;

    IF (SELECT COUNT(*) FROM stock WHERE group_id=_group_id && type=2 && product_id=_product_id) = 0 THEN
        RETURN 0;
    END IF;

    DECLARE _cur CURSOR FOR SELECT id, count FROM stock WHERE group_id=_group_id && type=2 && product_id=_product_id ORDER BY time DESC, id DESC;

    OPEN _cur;

    read_loop: LOOP
        FETCH _cur INTO _id, _sum_count;

        SET _running_count = _running_count + _sum_count;
        SET _current_id = _id;

        IF _running_count > _running_total_limit THEN
            LEAVE read_loop;
        END IF;
    END LOOP read_loop;

    CLOSE _cur;

    RETURN _current_id;
END $$

DELIMITER ;

error:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DECLARE _cur CURSOR FOR SELECT id, count FROM stock WHERE group_id=_group_id &amp;&amp; ' at line 12 
  • 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-27T19:15:43+00:00Added an answer on May 27, 2026 at 7:15 pm

    The following query:

    SELECT * FROM 
    (SELECT id, 
           count, 
           @running_count := @running_count + count AS Counter 
      FROM sumtest, (SELECT @running_count := 0) AS T1 ORDER BY id) AS TableCount 
    
    WHERE TableCount.Counter < 50;
    

    produces the results:

    id  count   Counter
    1   30      30
    2   10      40
    3   5       45
    

    I copied your table into MySql and called it “sumtest” btw. Please replace with your table name.

    Effectively, we work out the running total, in id order, then use that as a subquery.

    So this query:

    SELECT id, 
           count, 
           @running_count := @running_count + count AS Counter 
    FROM sumtest, (SELECT @running_count := 0) AS T1 
    ORDER BY id
    

    Produces:

    id  count   Counter
    1   30      30
    2   10      40
    3   5       45
    4   20      65
    5   15      80
    

    So then it becomes a trivial matter to select all those rows where the counter is less than your desired sum by performing another select on this.

    EDIT: Here is an example with a cursor. I’ve just thrown this function together for you (note my table is called sumtest and my account is the default root@localhost):

    DELIMITER $$
    
    DROP FUNCTION IF EXISTS `Test_Cursing` $$
    CREATE DEFINER=`root`@`localhost` FUNCTION `Test_Cursing`(_running_total_limit INT) RETURNS int
    BEGIN
      /* Why am I on StackOverflow at 01:41 on New Years Day. Dear oh dear, where's the beer? */
      DECLARE _running_count INT default 0;
      DECLARE _id INT;
      DECLARE _current_id INT;
      DECLARE _sum_count INT;
    
      DECLARE _cur CURSOR FOR SELECT id, count FROM sumtest ORDER BY id;
    
      OPEN _cur;
    
      read_loop: LOOP
        FETCH _cur INTO _id, _sum_count;
    
        SET _running_count = _running_count + _sum_count;
    
        IF _running_count > _running_total_limit   THEN
          LEAVE read_loop;
        END IF;
    
        SET _current_id = _id;
    
      END LOOP;
    
      CLOSE _cur;
    
        RETURN _current_id;
    
    END $$
    
    DELIMITER ;
    

    Calling it this way:

    SELECT Test_Cursing(50);
    

    will return id = 3 – that is, the last id before the running total limit is breached. You can then use this to:

     SELECT * FROM sumtest WHERE id <= Test_Cursing(50);
    

    Which returns:

    id  count
    1   30
    2   10
    3   5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want my query to return the rows of the table where a column
Let's say I have a query that return 10000 rows. Is it possible to
Is it possible to write a single query that can return two rows, one
Possible Duplicate: Return value from thread I want to get the free memory of
Is it possible to return a single value and an enumerable collection using LINQ
For my application most of my SQL queries return a specified number of rows.
I have a simple question regrading MySQL. Is it possible to return the rows
Is this possible to return all columns in query as empty column (not null)
Possible Duplicate: Is there an Oracle SQL query that aggregates multiple rows into one
I am wondering if it is possible to select a number of rows in

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.