力扣1126(MySQL)-查询活跃业务(中等)

题目:

事件表:Events

此表的主键是 (business_id, event_type)。

表中的每一行记录了某种类型的事件在某些业务中多次发生的信息。

问题
写一段 SQL 来查询所有活跃的业务。

如果一个业务的某个事件类型的发生次数大于此事件类型在所有业务中的平均发生次数,并且该业务至少有两个这样的事件类型,那么该业务就可被看做是活跃业务。

示例
查询结果格式如下所示:

Events table:

Result table:

‘reviews’、 ‘ads’ 和 ‘page views’ 的总平均发生次数分别是 (7+3)/2=5, (11+7+6)/3=8, (3+12)/2=7.5。
id 为 1 的业务有 7 个 ‘reviews’ 事件(大于 5)和 11 个 ‘ads’ 事件(大于 8),所以它是活跃业务。
建表语句:

1 drop table if EXISTS Events_1126;
2 create table if not exists Events_1126(
3 business_id int,
4 event_type varchar(50),
5 occurences int 
6 );
7 insert into Events_1126 values(1, 'reviews', 7),(3, 'reviews', 3),(1, 'ads', 11),(2, 'ads', 7),(3, 'ads', 6),(1, 'page views', 3),(2, 'page views', 12);

解题思路:

 ①先求出每种事件类型的平均次数;

1 select event_type, avg(occurences) as avg_num
2 from events_1126
3 group by event_type

 ②通过event_type将查询出来的临时表与events表进行联结;

1 select business_id,a.event_type,occurences,avg_num
2 from events_1126 a 
3 left join (
4     select event_type, avg(occurences) as avg_num
5     from events_1126
6     group by event_type
7     ) as b
8 on a.event_type = b.event_type

 ③最后以 occurences > avg_num 为条件进行筛选,按business_id分组之后,统计出 类型数大于2的business_id。

 1 select business_id
 2 from (
 3     select business_id,a.event_type,occurences,avg_num
 4     from events_1126 a 
 5     left join (
 6         select event_type, avg(occurences) as avg_num
 7         from events_1126
 8         group by event_type
 9     ) as b
10     on a.event_type = b.event_type
11 ) as temp
12 where occurences > avg_num
13 group by business_id
14 having count(*) >= 2

posted on 2023-04-13 10:42  我不想一直当菜鸟  阅读(50)  评论(0编辑  收藏  举报