Leetcode Problem 2688. Find Active Users

2688. Find Active Users

Leetcode Solutions

Using LEAD Window Function

  1. Use a CTE to select user_id, created_at, and the date of the next purchase using the LEAD window function.
  2. Partition the data by user_id and order by created_at to ensure the next purchase is for the same user and follows the current purchase chronologically.
  3. Calculate the date 7 days after each purchase using the DATE_ADD function.
  4. In the final SELECT statement, filter for rows where the next purchase date is on or after the current purchase date and on or before the date 7 days later.
  5. Select distinct user_ids that meet the active user criteria.

erDiagram
    Users {
        int user_id
        varchar item
        datetime created_at
        int amount
    }

Self-Join with Date Difference

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...