Leetcode Problem 1142. User Activity for the Past 30 Days II

1142. User Activity for the Past 30 Days II

Leetcode Solutions

Calculating Average Sessions Per User Using Aggregation and Date Filtering

  • Step 1: Filter the Activity table to include only the rows where activity_date is within the 30-day period ending on '2019-07-27'.
  • Step 2: Group the filtered results by user_id and count the distinct session_id for each user to get the number of sessions per user.
  • Step 3: Calculate the average number of sessions by dividing the total number of unique sessions by the number of unique users.
  • Step 4: Use the IFNULL function to handle cases where there might be no sessions, and default the value to 0.00.
  • Step 5: Use the ROUND function to round the average to two decimal places.

erDiagram
    Activity {
        int user_id
        int session_id
        date activity_date
        enum activity_type
    }

Calculating Average Sessions Per User with Subquery

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...