I have a table named activity
with 2 columns:
when as datetime // last time i saw a user
guid as varchar // a unique identifier for each user
This tables is used to track usage in a certain resource. Guid are created by the clients.
I want to be able to query how many new users I have in the last day/week/month.
A new user is identified by a guid
appearing in the activity table for the first time.
How can I do this in a single SQL statement?
update: The table contains an entry for every time a user is using the resource. So if he used 5 times, there will be 5 rows. Until a user uses the resource, i don't have his guid and there is no entry in table for him.
The result:
Thank you all, it helped a lot. For anyone interested, this is what I compiled from all your suggestions:
SET @duration=7;
SELECT COUNT(distinct guid) AS total_new_users FROM `activity`
WHERE `when` >= DATE_SUB(CURDATE(),INTERVAL @duration DAY) AND guid NOT IN
(
SELECT guid
FROM `activity`
WHERE `when` < DATE_SUB(CURDATE(),INTERVAL @duration DAY)
);