User Activity for past 30 days || Leetcode sql solution || simple and easy approach || Detailed Solution || 💯💯💯✅✅✅✅🔥🔥🔥🔥🔥
- Get link
- X
- Other Apps
1141. User Activity for the Past 30 Days I
Write an SQL query to find the daily active user count for a period of 30
days ending 2019-07-27
inclusively. A user was active on someday if they made at least one activity on that day.
Return the result table in any order.
The query result format is in the following example.
SQL solution :
# Write your MySQL query statement below
select activity_date as day , count(distinct user_id) as active_users
from Activity
where activity_date > date('2019-06-27') and activity_date <= date('2019-07-27')
group by activity_date
order by day asc;
Approach : (detailed solution)
We have to count the users datewise. Ensure to count users distinctly as a user can log in multiple times during a particular day. Also, we want to count between a specific period of date.
step 1 : SELECT activity_date, active_users columns to display
select activity_date as day , count(distinct user_id) as active_users from Activity
step 2 : Then apply where condition for 30 days ending '27-07-2019'
select activity_date as day , count(distinct user_id) as active_users from Activity
where activity_date > date('2019-06-27') and activity_date <= date('2019-07-27')
step 3 : grouping the results by activity_date and then aggregation is performed on
count ( distinct user_id )
select activity_date as day , count(distinct user_id) as active_users
from Activity
where activity_date > date('2019-06-27') and activity_date <= date('2019-07-27')
group by activity_date
Lastly, we can return the result table in any order
😊😊😊
😍😍😍
(●'◡'●)
Happy Learning :)
Comments
Post a Comment