I have two tables: users
, user_source_history
.
In table users
, all the users who have registered are saved. And, in table user_source_history
, their activities are stored. These activities may include log in to the system, ordering something. So one user can exist multiple times in user_source_history
.
Now the problem is I need to find all the active users in the bucket of 15 days.
So, for example, a user purchased something on 2014-12-03. Now, this user will appear active user till 2014-12-18.
I need to find all the active users in date range.
So, for example, in this date range-
2014-12-03 - 10 users are active.
2014-12-04 - 10(active on 2014-12-03) + (new users for this date)
2014-12-05 - Number on active user on this date + all users in 15 day bucket
2014-12-06 - Number on active user on this date + all users in 15 day bucket
The query:
SELECT CAST(`user_last_interaction_date` as Date)
FROM `users` U
LEFT JOIN `user_source_history` USH on U.user_id = USH.user_id
WHERE `user_last_interaction_date` >
(SELECT DATE_ADD(`user_last_interaction_date`, INTERVAL -15 DAY)
FROM `users`
GROUP BY CAST(`user_last_interaction_date` as Date)
)
GROUP BY CAST(`user_last_interaction_date` as Date)
I tried this query, but SQL says Subquery returns more than one row
How can I split the query to run for more than one row??