Query mysql add value day to previous days

1

I have the following query:

SELECT * FROM  vw_total_diario WHERE pessoa ='Daniel' GROUP BY data;

It returns as follows:

2017-05-02 Daniel 6
2017-05-03 Daniel 2
2017-05-04 Daniel 8
2017-05-05 Daniel 1
2017-05-08 Daniel 10
2017-05-09 Daniel 2
2017-05-10 Daniel 8
2017-05-11 Daniel 6
2017-05-12 Daniel 5
2017-05-15 Daniel 4

I need it to return like this (current day + previous days):

2017-05-02 Daniel 6
2017-05-03 Daniel 8
2017-05-04 Daniel 16
2017-05-05 Daniel 17
2017-05-08 Daniel 27
2017-05-09 Daniel 29
2017-05-10 Daniel 37
2017-05-11 Daniel 43
2017-05-12 Daniel 48
2017-05-15 Daniel 52

How could I mount this query?

    
asked by anonymous 16.05.2017 / 20:32

1 answer

1

You can use subquery to add up the amount available in previous days:

SELECT vtd.*,
       (SELECT SUM(vtd2.total)
          FROM vw_total_diario vtd2
         WHERE vtd2.pessoa = vtd.pessoa
           AND vtd2.data <= vtd.data
         GROUP BY vtd2.pessoa) as total_quantidade
  FROM  vw_total_diario vtd
 WHERE pessoa = 'Daniel'
 GROUP BY data;
    
16.05.2017 / 21:09