bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1126. Active Businesses

1126. Active Businesses

Leetcode Solutions

Using Subquery and Group By to Identify Active Businesses

  1. Create a subquery (aliased as avg_occurrences) to calculate the average occurrences for each event type using the AVG function and GROUP BY event_type.
  2. Perform an INNER JOIN between the Events table and the subquery on the event_type column.
  3. Use a WHERE clause to filter the results where the occurrences of an event for a business are greater than the average occurrences of that event type.
  4. Group the results by business_id using the GROUP BY clause.
  5. Use the HAVING clause to filter groups that have a count of distinct event types greater than 1, indicating active businesses.
  6. Select the business_id from the filtered groups as the final output.

erDiagram
    Events {
        int business_id
        varchar event_type
        int occurrences
        business_id event_type PK
    }

Using Window Function to Calculate Average and Identify Active Businesses

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...