I\'m trying to write a query to join a user table to an activity logging table and return the following for each user:
A) The time they last logged in.
B)
You can use a CASE statement in your COUNT:
SELECT A.UserID,
COUNT(
CASE WHEN TIME BETWEEN DATE_SUB( NOW( ) , INTERVAL 3 MONTH ) AND NOW( )
THEN Activity
END ) AS Logins,
MAX( TIME ) AS LastLogin
FROM UserMaster A
LEFT JOIN UserWebActivity B ON A.UserID = B.UserID
AND Activity = 'Login'
GROUP BY A.UserID
This solution however will be less efficient than @MarkByers if you have a large amount of data.