I'm having trouble getting a cumulative count in mysql. I need to count the amount of active registration accumulated in each month.
Table:
id | data_cadastro | data_exclusao
---+---------------+--------------
1 | 2018-07-01 | null
2 | 2018-07-02 | 2018-08-01
3 | 2018-08-01 | null
4 | 2018-08-02 | null
5 | 2018-08-03 | null
What I'm trying to get select to return this 'total' for month_an:
mes_ano | total | Descrição (só para entendimento...)
--------+------- | -------------------------------
2018-07 | 2 | No mês 07 haviam 02 cadastros ativos (o id 2 foi excluído só no mês 08, então ele conta para o mês 07);
2018-08 | 4 | No mês 08 haviam 04 (03 cadastros do mês 08 não excluídos + 01 cadastro ativo do mês 07);
With the help of other questions here of the forum I got the monthly count (not cumulative and without taking the exclusions) like this:
SELECT
DATE_FORMAT(data_cadastro,'%Y-%m') AS 'mes_ano',
COUNT(id) AS 'total'
FROM cadastro
GROUP BY DATE_FORMAT(data_cadastro,'%Y-%m')
I am in doubt if it is possible to do this criterion directly in SELECT or take the complete query for PHP and treat the information there (I do not know what is best for the performance as well) ... If someone knows how to help me . :)