sql >> Base de Datos >  >> RDS >> Mysql

Acumulación de sumas usando MySQL

SELECT x.*
     , x.cf1+x.cf2 sub_total
     , SUM(y.cf1+y.cf2) running
  FROM 1_bugs x 
  JOIN 1_bugs y 
    ON y.id <= x.id 
 GROUP 
    BY x.id;
+----+------------+-----+-----+-----------+---------+
| id | date       | cf1 | cf2 | sub_total | running |
+----+------------+-----+-----+-----------+---------+
|  1 | 2016-07-19 |   3 |   2 |         5 |       5 |
|  2 | 2016-07-19 |   2 |   1 |         3 |       8 |
|  3 | 2016-07-22 |   2 |   2 |         4 |      12 |
+----+------------+-----+-----+-----------+---------+