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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:07:43+00:00 2026-05-26T19:07:43+00:00

I have following data in a mysql table, +————+——–+——–+ | date | inQty |

  • 0

I have following data in a mysql table,

+------------+--------+--------+
| date       | inQty  | outQty |
+------------+--------+--------+
| 2011-10-24 | 700.00 | 0.0    |
| 2011-10-01 | 500.00 | 0.0    |
| 2011-10-02 | 500.00 | 0.0    |
| 2011-10-03 | 550.00 | 0.0    |
| 2011-10-04 | 100.00 | 0.0    |
| 2011-10-05 | 200.00 | 0.0    |
| 2011-10-05 |   0.00 | 100.0  |
| 2011-10-02 |   0.00 | 500.0  |
| 2011-10-03 |   0.00 | 150.0  |
| 2011-10-24 | 200.00 | 0.0    |
+------------+--------+--------+

from above table I need following results,

+------------+--------+---------+----------+
| tDate      | tInQty | tOutQty | tbalance |
+------------+--------+---------+----------+
| 2011-10-01 | 500.00 |    0.00 |   500.00 | 
| 2011-10-02 | 500.00 |    0.00 |   1000.00| ->> (tInQty + tInQty)
| 2011-10-02 |   0.00 |  500.00 |   500.00 |->> (tInQty - tOutQty)
| 2011-10-03 | 550.00 |    0.00 |   1050.00|
| 2011-10-03 |   0.00 |  150.00 |   900.00 |
| 2011-10-04 | 100.00 |    0.00 |   1000.99|
| 2011-10-05 | 200.00 |    0.00 |   1200.99|
| 2011-10-05 |   0.00 |  100.00 |   1100.00|
| 2011-10-24 | 700.00 |    0.00 |   1800.00|
| 2011-10-24 | 200.00 |    0.00 |   200.00 |
+------------+--------+---------+----------+

to obtain above result I wrote following stored procedure,

-- --------------------------------------------------------------------------------
-- Routine DDL
-- --------------------------------------------------------------------------------
DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `get_balance`()
BEGIN




DECLARE vDate DATE DEFAULT '0000-00-00';
DECLARE vInQty DECIMAL(5,2) DEFAULT 0.0;
DECLARE vOutQty DECIMAL(5,2) DEFAULT 0.0;
DECLARE balance DECIMAL(5,2) DEFAULT 0.0;


DECLARE vvDate INT;
DECLARE vSum DECIMAL(5,2) DEFAULT 0.0;

DECLARE flag INT DEFAULT 0;

DECLARE tCursor CURSOR FOR SELECT * FROM `mydatabase`.`new_table` ORDER BY date;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET flag = 1;


DROP TABLE IF EXISTS temp_table;

CREATE TEMPORARY TABLE temp_table(
    tDate DATE NOT NULL DEFAULT '0000-00-00',
    tInQty DECIMAL(5,2) DEFAULT 0.0,
    tOutQty DECIMAL(5,2) DEFAULT 0.0,
    tbalance DECIMAL(5,2) DEFAULT 0.0
);



OPEN tCursor;
SET balance = 0.0;


REPEAT
FETCH tCursor INTO vDate, vInQty, vOutQty;
SELECT COUNT(vDate) INTO vvDate FROM temp_table WHERE vDate = tDate;

IF vvDate = 0 THEN

    CASE
        WHEN vInQty != 0.0 THEN

            SET balance = balance + vInQty;
            INSERT INTO temp_table VALUES(vDate, vInQty, vOutQty, balance);


        WHEN vOutQty != 0.0 THEN
            SET balance = balance - vOutQty;
            INSERT INTO temp_table VALUES(vDate, vInQty, vOutQty, balance);

        ELSE
            SET vSum = 0.0;

    END CASE; -- end of case

ELSEIF vvDate > 0 THEN
    CASE

        WHEN vInQty != 0.0 THEN
            SET balance = balance + vInQty;
            INSERT INTO temp_table VALUES(vDate, vInQty, vOutQty, vInQty);

        WHEN vOutQty != 0.0 THEN
            SET balance = balance + vOutQty;
            INSERT INTO temp_table VALUES(vDate, vInQty, vOutQty, balance);

        ELSE
            SET vSum = 0.0;
    END CASE; -- end of case
END IF;

UNTIL flag END REPEAT; -- End of repeat

CLOSE tCursor;


SELECT * FROM temp_table;

END

But, when I run above stored procedure I get following result, how can I obtain the result I am expecting?

+------------+--------+---------+----------+
| tDate      | tInQty | tOutQty | tbalance |
+------------+--------+---------+----------+
| 2011-10-01 | 500.00 |    0.00 |   500.00 |
| 2011-10-02 | 500.00 |    0.00 |   999.99 |
| 2011-10-02 |   0.00 |  500.00 |   999.99 |
| 2011-10-03 | 550.00 |    0.00 |   999.99 |
| 2011-10-03 |   0.00 |  150.00 |   999.99 |
| 2011-10-04 | 100.00 |    0.00 |   999.99 |
| 2011-10-05 | 200.00 |    0.00 |   999.99 |
| 2011-10-05 |   0.00 |  100.00 |   999.99 |
| 2011-10-24 | 700.00 |    0.00 |   999.99 |
| 2011-10-24 | 200.00 |    0.00 |   200.00 |
| 2011-10-24 | 200.00 |    0.00 |   200.00 |
+------------+--------+---------+----------+
  • 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-26T19:07:44+00:00Added an answer on May 26, 2026 at 7:07 pm

    Firstly, I think that your desired result is:

    +------------+--------+---------+----------+
    | tDate      | tInQty | tOutQty | tbalance |
    +------------+--------+---------+----------+
    | 2011-10-01 | 500.00 |    0.00 |   500.00 | 
    | 2011-10-02 | 500.00 |    0.00 |   1000.00| ->> (tInQty + tInQty)
    | 2011-10-02 |   0.00 |  500.00 |   500.00 |->> (tInQty - tOutQty)
    | 2011-10-03 | 550.00 |    0.00 |   1050.00|
    | 2011-10-03 |   0.00 |  150.00 |   900.00 |
    | 2011-10-04 | 100.00 |    0.00 |   1000.00|
    | 2011-10-05 | 200.00 |    0.00 |   1200.00|
    | 2011-10-05 |   0.00 |  100.00 |   1100.00|
    | 2011-10-24 | 700.00 |    0.00 |   1800.00|
    | 2011-10-24 | 200.00 |    0.00 |   2000.00|
    +------------+--------+---------+----------+
    

    In order to get that you don’t have to write a stored procedure. It can be achieved using
    MySQL user defined variables. For example:

    create table dateCumulative
    (vDate date not null,
    inQty decimal (12,2) not null default 0.0,
    outQty decimal (12,2) not null default 0.0
    );
    
    insert into dateCumulative values ('2011-10-24',700.00,0.0);
    insert into dateCumulative values ('2011-10-01',500.00,0.0);
    insert into dateCumulative values ('2011-10-02',500.00,0.0);
    insert into dateCumulative values ('2011-10-03',550.00,0.0);
    insert into dateCumulative values ('2011-10-04',100.00,0.0);
    insert into dateCumulative values ('2011-10-05',200.00,0.0);
    insert into dateCumulative values ('2011-10-05',0.00,100.0);
    insert into dateCumulative values ('2011-10-02',0.00,500.0);
    insert into dateCumulative values ('2011-10-03',  0.00    ,150.0);
    insert into dateCumulative values ('2011-10-24',  200.00  ,0.0);
    
    select t.vDate,t.inQty,t.outQty,
    round(t.inQtySum-t.outQtySum,2) as balance
    from
    ( 
    select 
    @inCsum := @inCsum + inQty as inQtySum,
    @outCsum := @outCsum + outQty as outQtySum,
    dc.*
    from dateCumulative dc
    cross join (SELECT @curDate := '1970-01-01',@inCsum:=0,@outCsum:=0) as t
    order by vDate asc
    ) t;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL table with the following data (simplified): INSERT INTO `stores` (`storeId`,
i have the following mysql table.... ----------------------- id customer_name date ----------------------- now let me
I have a MySQL table with the following columns: id(int), date (timestamp), starttime(varchar), endtime(varchar),
I have a php variable: $foo My MySQL table called data has the following
So, I have got following data in a MySQL table: mysql> SELECT * FROM
So im having a problem (obviously). I have the following MySQL table data 7
I have a MySQL table which contains the following type of information: Date product
I have a MySQL table with following data (sample); UserId - DeviceId - StartDate
I have the following MySql table containing my raw event data (about 1.5 million
I have the following mysql table called test. lets say it has the data

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.