返回介绍

solution / 1100-1199 / 1126.Active Businesses / README_EN

发布于 2024-06-17 01:03:23 字数 3180 浏览 0 评论 0 收藏 0

1126. Active Businesses

中文文档

Description

Table: Events

+---------------+---------+
| Column Name   | Type  |
+---------------+---------+
| business_id   | int   |
| event_type  | varchar |
| occurrences   | int   | 
+---------------+---------+
(business_id, event_type) is the primary key (combination of columns with unique values) of this table.
Each row in the table logs the info that an event of some type occurred at some business for a number of times.

The average activity for a particular event_type is the average occurrences across all companies that have this event.

An active business is a business that has more than one event_type such that their occurrences is strictly greater than the average activity for that event.

Write a solution to find all active businesses.

Return the result table in any order.

The result format is in the following example.

 

Example 1:

Input: 
Events table:
+-------------+------------+-------------+
| business_id | event_type | occurrences |
+-------------+------------+-------------+
| 1       | reviews  | 7       |
| 3       | reviews  | 3       |
| 1       | ads    | 11      |
| 2       | ads    | 7       |
| 3       | ads    | 6       |
| 1       | page views | 3       |
| 2       | page views | 12      |
+-------------+------------+-------------+
Output: 
+-------------+
| business_id |
+-------------+
| 1       |
+-------------+
Explanation:  
The average activity for each event can be calculated as follows:
- 'reviews': (7+3)/2 = 5
- 'ads': (11+7+6)/3 = 8
- 'page views': (3+12)/2 = 7.5
The business with id=1 has 7 'reviews' events (more than 5) and 11 'ads' events (more than 8), so it is an active business.

Solutions

Solution 1

# Write your MySQL query statement below
SELECT business_id
FROM
  EVENTS AS t1
  JOIN (
    SELECT
      event_type,
      AVG(occurences) AS occurences
    FROM EVENTS
    GROUP BY event_type
  ) AS t2
    ON t1.event_type = t2.event_type
WHERE t1.occurences > t2.occurences
GROUP BY business_id
HAVING COUNT(1) > 1;

Solution 2

# Write your MySQL query statement below
WITH
  T AS (
    SELECT
      business_id,
      occurences > AVG(occurences) OVER (PARTITION BY event_type) AS mark
    FROM Events
  )
SELECT business_id
FROM T
WHERE mark = 1
GROUP BY 1
HAVING COUNT(1) > 1;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文