Leetcode Problem 2051. The Category of Each Member in the Store

2051. The Category of Each Member in the Store

Leetcode Solutions

Left Join and Aggregation with Conditional Logic

  1. Perform a LEFT JOIN from Members to Visits on member_id to include all members, even those without visits.
  2. Perform another LEFT JOIN from Visits to Purchases on visit_id to include all visits, even those without purchases.
  3. Use GROUP BY to group the results by member_id and name.
  4. Use COUNT() to count the number of visits and purchases for each member.
  5. Calculate the conversion rate using the formula (100 * COUNT(purchases)) / COUNT(visits).
  6. Use a CASE statement to assign categories based on the conversion rate.
  7. Select the member_id, name, and the calculated category for the final output.
erDiagram
    Members ||--o{ Visits : has
    Visits ||--o{ Purchases : has
    Members {
        int member_id PK
        varchar name
    }
    Visits {
        int visit_id PK
        int member_id FK
        date visit_date
    }
    Purchases {
        int visit_id PK
        int charged_amount
    }

Subqueries and Conditional Aggregation

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...