Mysql Sum up to each day of the month

594 views Asked by At

Get sum up to each day of the month

Here is my table called payment

amount  | date_added
===================================
30.00           |2016-12-05 20:48:13
-10             |2016-12-10 17:39:45
-55.30          |2016-12-10 17:55:18
78.00           |2016-12-11 14:50:50
-20.00          |2016-12-11 15:50:50
-30.00          |2016-12-22 01:47:22
300.00          |2016-12-23 01:01:36
-40             |2016-12-24 02:10:00

I tried the following query on it but it gives sum of each day.

SELECT SUM(`amount`) as total_amount, `date_added`  FROM `payment` where (`payment`.`date_added` between '2016-12-1'  and '2016-12-30') GROUP BY  DATE(date_added)

The result was

total_amount    | date_added
===================================
30.00       |2016-12-05 20:48:13
-65.30      |2016-12-10 17:39:45
58.00       |2016-12-11 14:50:50
-30.00      |2016-12-22 01:47:22
300.00      |2016-12-23 01:01:36
-40.00      |2016-12-24 02:10:00

What I want is sum up to the each day of the month. NOT the sum of each day.

I want the following result with Mysql.

total_amount    | date_added
===================================
30.00       |2016-12-05 20:48:13
-35.30      |2016-12-10 17:39:45
22.70       |2016-12-11 14:50:50
-7.30       |2016-12-22 01:47:22
292.70      |2016-12-23 01:01:36
252.70      |2016-12-24 02:10:00
1

There are 1 answers

0
Niroshan Ranapathi On

Test this code. after second table. Then you can write best query. in here table t is a second table

SELECT (SELECT SUM(x.count)
            FROM TABLE x
           WHERE x.id <= t.total_amount) AS total_amount
    FROM TABLE t
ORDER BY t.total_amount