date - Run SQL queries by month on MySQL workbench? -


i working on mysql db mysql workbench.

my objective retrieve signups database establish company's kpis on excel spreadsheet.

i wrote sql queries worked want set complete 1 in order avoid using xxx different queries.

to signups each month (based on 'created_at'), makes job:

select year(u.created_at) year, monthname(u.created_at) month, count(distinct u.id) 'new shoppers signups'  users u group year, month order u.created_at 

but wanted have the total of previous signups each month

jan : 12 feb : 14 (12 + 2 new signups) march : 22 (14 + 8 new signups) ...  

where sum of previous signups

i thinking like:

declare @month = '2012-01-01' //startdate while @month < curdate()  begin     select count(distinct u.id)     u.created_at < @month     dateadd(month, 1, @month) // incrementing next month end 

but neither while loop, declare, set, or date function work on mysql workbench.

i heard have declare procedures didn't have more success...

i know use excel result, want improve use of sql , make clear work.

you close answer. take results , make inner query. basis of outer query using mysql variables accumulate each row.

select       pq.yearadded,        pq.monthadded,        pq.newshoppers 'new shoppers signups',       @runbal := @runbal + pq.newshoppers totalnewshoppers          ( select                year(u.created_at) yearadded,                monthname(u.created_at) monthadded,                count(distinct u.id) newshoppers                          users u            group                year(u.created_at),                monthname(u.created_at)            order                year(u.created_at),                monthname(u.created_at) ) pq,       ( select @runbal := 0 ) sqlvars 

i suggest having column names stay away possible reserved words, such year, month , other standard sql commands , function names... otherwise typically need add tick-marks around column names


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -