Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DROP TABLE IF EXISTS transactions;
- CREATE TABLE transactions(
- id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
- tgl DATE,
- amount INT null
- ) ENGINE=MyISAM;
- INSERT INTO transactions VALUES
- (1,'2020-05-30',5200),
- (2,'2020-05-31',1500),
- (3,'2020-06-01',3200),
- (4,'2020-06-02',3500),
- (5,'2020-06-03',1200),
- (6,'2020-06-04',5200),
- (7,'2020-06-05',7300),
- (8,'2020-06-06',3400),
- (9,'2020-06-07',4900),
- (10,'2020-06-08',8600),
- (11,'2020-06-09',1700),
- (12,'2020-06-10',5500);
- select * from transactions;
- +----+------------+--------+
- | id | tgl | amount |
- +----+------------+--------+
- | 1 | 2020-05-30 | 5200 |
- | 2 | 2020-05-31 | 1500 |
- | 3 | 2020-06-01 | 3200 |
- | 4 | 2020-06-02 | 3500 |
- | 5 | 2020-06-03 | 1200 |
- | 6 | 2020-06-04 | 5200 |
- | 7 | 2020-06-05 | 7300 |
- | 8 | 2020-06-06 | 3400 |
- | 9 | 2020-06-07 | 4900 |
- | 10 | 2020-06-08 | 8600 |
- | 11 | 2020-06-09 | 1700 |
- | 12 | 2020-06-10 | 5500 |
- +----+------------+--------+
- SELECT x.tgl
- , x.amount
- , SUM(y.amount) AS accu
- FROM
- (
- SELECT *
- FROM transactions
- ) x
- JOIN
- (
- SELECT *
- FROM transactions
- ) y
- ON (y.tgl <= x.tgl AND DATE_FORMAT(y.tgl,'%Y%m')=DATE_FORMAT(x.tgl,'%Y%m'))
- GROUP
- BY x.tgl,x.amount;
- +------------+--------+-------+
- | tgl | amount | accu |
- +------------+--------+-------+
- | 2020-05-30 | 5200 | 5200 |
- | 2020-05-31 | 1500 | 6700 |
- | 2020-06-01 | 3200 | 3200 |
- | 2020-06-02 | 3500 | 6700 |
- | 2020-06-03 | 1200 | 7900 |
- | 2020-06-04 | 5200 | 13100 |
- | 2020-06-05 | 7300 | 20400 |
- | 2020-06-06 | 3400 | 23800 |
- | 2020-06-07 | 4900 | 28700 |
- | 2020-06-08 | 8600 | 37300 |
- | 2020-06-09 | 1700 | 39000 |
- | 2020-06-10 | 5500 | 44500 |
- +------------+--------+-------+
Advertisement
Add Comment
Please, Sign In to add comment