I assume connections.connection_date & users.creation_date are of datetime.
To get the percentage of connected users for a month against the total users as of to that month, use:
SELECT u.user_group, DATE_FORMAT(`c`.`connection_date`, "%M %Y") AS month,
COUNT(DISTINCT u.`user_id`) / (SELECT COUNT(`user_id`) FROM users WHERE users.creation_date <= adddate(last_day(`c`.`connection_date`), 1) AND users.user_group = u.user_group) AS percentage,
COUNT(DISTINCT u.`user_id`) as loggedThisMonth,
(SELECT COUNT(`user_id`) FROM users WHERE users.creation_date <= adddate(last_day(`c`.`connection_date`), 1) AND users.user_group = u.user_group) AS totalRegisteredToMonth
FROM connections c LEFT JOIN users u ON c.`user_id` = u.`user_id`
GROUP BY u.user_group, DATE_FORMAT(`c`.`connection_date`, "%M %Y")
ORDER BY DATE_FORMAT(`c`.`connection_date`, "%Y %m"), u.user_group ASC
This works by:
- counting the DISTINCT users.user_id that have connected each month, thus preventing recounted users with multiple connections in a month
COUNT(DISTINCT u.user_id)
- using a subselect to calculate the registered users of a user group till that month
(SELECT COUNT(
user_id) FROM users WHERE users.creation_date <= adddate(last_day(
c.
connection_date), 1) AND users.user_group = u.user_group)
Reference:
adddate(last_day(`c`.`connection_date`), 1)
return the 1st day of the next month date