事件表:Events
Column Name | Type |
---|---|
business_id | int |
event_type | varchar |
occurences | int |
平均活动 是指有特定 event_type 的具有该事件的所有公司的 occurences 的均值。
活跃业务 是指具有 多个 event_type 的业务,它们的 occurences 严格大于 该事件的平均活动次数。
写一个解决方案,找到所有 活跃业务。
以 任意顺序 返回结果表。
结果格式如下所示。
示例 1:
输入:
Events 表:
business_id | event_type | occurences |
---|---|---|
1 | reviews | 7 |
3 | reviews | 3 |
1 | ads | 11 |
2 | ads | 7 |
3 | ads | 6 |
1 | page views | 3 |
2 | page views | 12 |
输出:
business_id |
---|
1 |
解释:
每次活动的平均活动可计算如下:
1、occurences 大于平均活动次数,求每种活动的平均活动次数
2、多个 event_type 的业务,所以是有两个或以上就是活跃的业务
可以借鉴下我下面写的代码
SELECT event_type, SUM(occurences)/COUNT(*) AS num
FROM Events
GROUP BY event_type
可以换成
SELECT event_type, AVG(occurences) AS num
FROM Events
GROUP BY event_type
AVG(occurences) = SUM(occurences)/COUNT(*)
AVG函数
与 GROUP BY子句
一起计算表中每组行的平均值using() 用于两张表的 join 查询,要求 using() 指定的列在两个表中均存在,并使用之用于 join 的条件
示例:select a.*, b.* from a left join b using(colA);
等同于:select a.*, b.* from a left join b on a.colA = b.colA;
参考:MySQL USING关键词 / USING()函数的使用
SELECT business_id
FROM Events one
LEFT JOIN (
SELECT event_type, SUM(occurences)/COUNT(*) AS num
FROM Events
GROUP BY event_type
) AS two USING(event_type)
WHERE one.occurences > two.num
GROUP BY one.business_id
HAVING COUNT(one.business_id) >= 2
SELECT business_id
FROM (
SELECT *, AVG(occurences) OVER (PARTITION BY event_type) avg_oc
FROM Events
) t1
WHERE occurences > avg_oc
GROUP BY business_id
HAVING COUNT(distinct event_type) >= 2
SELECT event_type, SUM(occurences)/COUNT(*) AS num
FROM Events
GROUP BY event_type
| event_type | num |
| ---------- | --- |
| reviews | 5 |
| ads | 8 |
| page views | 7.5 |
SELECT *, AVG(occurences) OVER (PARTITION BY event_type) avg_oc
FROM Events
| business_id | event_type | occurences | avg_oc |
| ----------- | ---------- | ---------- | ------ |
| 1 | ads | 11 | 8 |
| 2 | ads | 7 | 8 |
| 3 | ads | 6 | 8 |
| 1 | page views | 3 | 7.5 |
| 2 | page views | 12 | 7.5 |
| 1 | reviews | 7 | 5 |
| 3 | reviews | 3 | 5 |