Leetcode 1126. Active Businesses | SQL Pandas 解法

该博客介绍了如何通过SQL和Pandas解决Leetcode上的问题1126,寻找那些拥有超过平均事件发生次数的活跃企业。文章详细阐述了计算每个事件平均发生次数的思路,并给出了使用窗口函数和transform计算的代码示例。
摘要由CSDN通过智能技术生成

找到活跃的商家

Write an SQL query to find all active businesses.

An active business is a business that has more than one event type with occurences greater than the average occurences of that event type among all businesses.

Table: Events
+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| business_id   | int     |
| event_type    | varchar |
| occurences    | int     | 
+---------------+---------+
(business_id, event_type) is the primary key of this table.
Each row in the table logs the info that an event of some type occured at 
some business for a number of times.
例子:
Events table:
+-------------+------------+------------+
| 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         |
+-------------+------------+------------+

解法

思路:

  • 计算新的列叫做average occurences,代表每个event的平均出现次数
  • 筛选大于平均值的数据,即occurences > average occurences
  • group by business_id, 找出event type数量大于1的商家

SQL

用window function计算average occurences

with 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值