I have users table with a flag of free user or paid users and with date of registration. Something like
+------------+-----------+--------------+
| USERNAME | FREE_USER | DATE_OF_REG |
+------------+-----------+--------------+
| user_name1 | y | 2018-01-14 |
| user_name2 | n | 2017-12-23 |
| user_name3 | y | 2017-12-12 |
| user_name4 | y | 2017-11-19 |
| user_name5 | n | 2017-09-13 |
| user_name6 | y | 2017-08-27 |
| user_name7 | n | 2017-07-06 |
+------------+-----------+--------------+
I need count of free_users and paid_users for last 6 months and if there is no users registered in a month it should give 0 as free and paid count.
I want to create line chart for both free user and paid users as like in this example (http://www.chartjs.org/samples/latest/charts/line/basic.html). For this I need data in comma (,) separated format like:
month_of_registration : ['August', 'September', 'October', 'November', 'December', 'January']
free_users_count_for_related_month : [0, 1, 0, 1, 0, 1, 1]
paid_users_count_for_related_month : [1, 0, 1, 0, 0, 1, 0]
How can I achieve this with mysql query in PHP.
I have written this query:
SELECT MONTH(date_of_reg) as monthno, MONTHNAME(date_of_reg) as month_name, YEAR(date_of_reg) as year,
COUNT(CASE WHEN `free_user` = 'y' THEN 1 END) AS free_user,
COUNT(CASE WHEN `free_user` = 'n' THEN 1 END) AS paid,
COUNT(CASE WHEN `free_user` != '' THEN 1 END) as total_users
FROM tbl_agents GROUP BY MONTH( date_of_reg ) ORDER BY year, monthno
But I didn't achieve which I want.
SELECT MONTH(date_of_reg) as monthno, MONTHNAME(date_of_reg) as month_name, YEAR(date_of_reg) as year, COUNT(CASE WHENfree_user` = 'y' THEN 1 END) AS free_user, COUNT(CASE WHENfree_user= 'n' THEN 1 END) AS paid, COUNT(CASE WHENfree_user!= '' THEN 1 END) as total_users FROM tbl_agents GROUP BY MONTH( date_of_reg ) ORDER BY year, monthno` but didn't achieve what I actually want.