mysql - Sum of Counts from successive dates -


i need total number of item every month.

so far, following code :

select count(mpay_collector_company.id) `number of collector companies`,   month(mpay_collector_company.created_at) month,   year(mpay_collector_company.created_at) year mpay_collector_company group month(mpay_collector_company.created_at),   year(mpay_collector_company.created_at) 

i have following response :

#|year|month ------------ 5|2014|11 3|2014|12 3|2015|1 7|2015|2 6|2015|3 2|2015|4 5|2015|6 1|2015|7 

and instead of number each month i'd have sum beginning each month, :

sum|year|month --------------   5|2014|11   8|2014|12  11|2015|1  18|2015|2  24|2015|3  26|2015|4  31|2015|6  32|2015|7 

any ideas ?

edit : request implemented view, sub-requests pretty no-go :x

something work:

select count(distinct previous_mpay_collector_company.id)+ count(distinct mpay_collector_company.id) `number of collector companies`,        month(mpay_collector_company.created_at) month,        year(mpay_collector_company.created_at) year mpay_collector_company   left join mpay_collector_company previous_mpay_collector_company     on   year(mpay_collector_company.created_at) > year(previous_mpay_collector_company.created_at)  or (month(mpay_collector_company.created_at) > month(previous_mpay_collector_company.created_at)          ,  year(mpay_collector_company.created_at) >= year(previous_mpay_collector_company.created_at))   , previous_mpay_collector_company.id <> mpay_collector_company.id group month(mpay_collector_company.created_at), year(mpay_collector_company.created_at); 

the trick self joining of previous months , counting joined table.


Comments