C++ || STL || standard template libraries

Image
                                       C++ STL  1.      Pair :  Syntax :      pair <int,char> p1;     it creates a pair {2,'c'} like this.           pair <int,int> p2;     it creates a pair {2,3} like this .  Basic Code :  #include <bits/stdc++.h> using namespace std; int main() {     // cout<<"hello world\n";     pair <int,int> p1 = {1,2};     cout<<p1.first<< " "<< p1.second<<endl;          pair <int, pair<int ,char>> p2 = {1,{2,'c'}};     cout<<p2.first<<" "<<p2.second.first<<" "<<p2.second.second<<endl;          pair <int,int> arr[] = {{2,3},{4,5},{6,7}};     cout...

User Activity for past 30 days || Leetcode sql solution || simple and easy approach || Detailed Solution || 💯💯💯✅✅✅✅🔥🔥🔥🔥🔥

                             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

Popular posts from this blog

1050. Actors and Directors Who Cooperated At Least Three Times || Leetcode sql solution || Simple and Easy Approach || ✅✅✅✅💯💯💯🔥🔥🔥🔥

Power of Millets || Happy And Healthy Life

Typing Lesson PART-3