So let's say we have a table referrals
structured with the following columns:
(string) referral_id, (string) referrer, (string) user_id, (int) timestamp, (int) condition
What I want is at any given moment, the number of distinct users for which condition
> 0, grouped by referrer. In addition, if a user has condition
> 0 by multiple referrers, only the most recent referral should be counted. So, if both 'Referrer A' and 'Referrer B' referred a given user but Referrer B's was more recent, only Referrer B would get the user added to their count.
I know that in order to get the counts by referrer, I would do
SELECT referrer, COUNT(DISTINCT(user_id))
FROM referrals
WHERE condition > 0
GROUP BY referrer
How would I add in the uniqueness and most recent referral conditions?