Incremental counts in mysql -
i have table "userlogins" , when user login system drop record in "userlogins" table.
jan 10th (users : 1,2,3,4) // 4 records ids 1,2,3,4 jan 20th (1,2,3,4,5,6) // 6 records ids 1,2,3,4,5,6 jan 30th (1,2) feb 10th (1,7) feb 20th (2,6,8) feb 25th (1,2,3,5) mar 10th (3,4) mar 20th (4,5,9) mar 30th (8,10,11) apr 10th (10,12) apr 20th (1,2,3,6,13, 14, 15) apr 30th (11,12,16)
when write group results follows
jan - 6 feb - 7 mar - 7 apr - 11
but need out put follows
upto jan - 6 //count of distinct users upto jan upto feb - 8 //count of distinct users upto feb upto mar - 11 //count of distinct users upto mar upto apr - 16 //count of distinct users upto apr
your first count this:
select date_format(login_date, '%y-%b') year_month, count(distinct user_id) userlogins group date_format(login_date, '%y-%b')
while count users given mount, use join:
select date_format(last_day, '%y-%b') year_month, count(distinct user_id) (select distinct last_day(login_date) last_day userlogins) d inner join userlogins on userlogins.login_date<=last_day group date_format(last_day, '%y-%b')
on subquery d return last days of every month has record on userlogins table, join userlogin logged end of month, , count.
Comments
Post a Comment