I have a system where users log in from different companies. I'm trying to run a query to see the number of active users vs total number of users for each company.
Users table:
userID, companyID, lastLogin
Desired Output:
companyID, totalCompanyUsers, numUsersWhoLoggedInWithinLastMonthFromCompany
Query attempt:
SELECT companyID, COUNT(userID) AS `numUsersWhoLoggedInWithinLastMonth`
FROM Users
WHERE IFNULL(TIMESTAMPDIFF(MONTH, lastLogin, NOW()),- 1) = 1
GROUP BY companyID;
I'm struggling to figure out how to have two aggregation functions, where one is conditional.