In social network like Facebook or Twitter, people send friend requests and accept others’ requests as well. Now given two tables as below:

Table: friend_request

| sender_id | send_to_id |request_date|
|-----------|------------|------------|
| 1 | 2 | 2016_06-01 |
| 1 | 3 | 2016_06-01 |
| 1 | 4 | 2016_06-01 |
| 2 | 3 | 2016_06-02 |
| 3 | 4 | 2016-06-09 |

Table: request_accepted

| requester_id | accepter_id |accept_date |
|--------------|-------------|------------|
| 1 | 2 | 2016_06-03 |
| 1 | 3 | 2016-06-08 |
| 2 | 3 | 2016-06-08 |
| 3 | 4 | 2016-06-09 |
| 3 | 4 | 2016-06-10 |

Write a query to find the overall acceptance rate of requests rounded to 2 decimals, which is the number of acceptance divide the number of requests.

For the sample data above, your query should return the following result.

|accept_rate|
|-----------|
| 0.80|

Note:

  • The accepted requests are not necessarily from the table friend_request. In this case, you just need to simply count the total accepted requests (no matter whether they are in the original requests), and divide it by the number of requests to get the acceptance rate.
  • It is possible that a sender sends multiple requests to the same receiver, and a request could be accepted more than once. In this case, the ‘duplicated’ requests or acceptances are only counted once.
  • If there is no requests at all, you should return 0.00 as the accept_rate.

Explanation: There are 4 unique accepted requests, and there are 5 requests in total. So the rate is 0.80.

Follow-up:

    • Can you write a query to return the accept rate but for every month?
    • How about the cumulative accept rate for every day?

Intuition

Count the accepted requests and then divides it by the number of all requests.

Algorithm

To get the distinct number of accepted requests, we can query from the request_accepted table.

select count(*) from (select distinct requester_id, accepter_id from request_accepted;

With the same technique, we can have the total number of requests from the friend_request table:

select count(*) from (select distinct sender_id, send_to_id from friend_request;

At last, divide these two numbers and round it to a scale of 2 decimal places to get the required acceptance rate.

Wait! The divisor (total number of requests) could be '0' if the table friend_request is empty. So, we have to utilize ifnull to deal with this special case.

解法1:

select
round(
ifnull(
(select count(*) from (select distinct requester_id, accepter_id from request_accepted) as A)
/
(select count(*) from (select distinct sender_id, send_to_id from friend_request) as B),
0)
, 2) as accept_rate;  

解法2:

select coalesce(round
(count(distinct requester_id, accepter_id)
/
count(distinct sender_id, send_to_id),2),
0)
as accept_rate
from friend_request, request_accepted

  

All LeetCode Questions List 题目汇总

[LeetCode] 597. Friend Requests I: Overall Acceptance Rate 朋友请求 I: 全部的接受率的更多相关文章

  1. python+requests实现接口测试 - get与post请求使用(转载)

    转自:http://www.cnblogs.com/nizhihong/p/6567928.html 简介:Requests 是用Python语言编写,基于 urllib,采用 Apache2 Lic ...

  2. python+requests实现接口测试 - get与post请求使用

    简介:Requests 是用Python语言编写,基于 urllib,采用 Apache2 Licensed 开源协议的 HTTP 库.它比 urllib 更加方便,可以节约我们大量的工作,完全满足 ...

  3. python 爬虫 基于requests模块发起ajax的post请求

    基于requests模块发起ajax的post请求 需求:爬取肯德基餐厅查询http://www.kfc.com.cn/kfccda/index.aspx中指定某个城市地点的餐厅数据 点击肯德基餐厅查 ...

  4. python 爬虫 基于requests模块发起ajax的get请求

    基于requests模块发起ajax的get请求 需求:爬取豆瓣电影分类排行榜 https://movie.douban.com/中的电影详情数据 用抓包工具捉取 使用ajax加载页面的请求 鼠标往下 ...

  5. [LeetCode] 602. Friend Requests II: Who Has Most Friend? 朋友请求 II: 谁有最多的朋友?

    In social network like Facebook or Twitter, people send friend requests and accept others' requests ...

  6. [LeetCode] Friends Of Appropriate Ages 适合年龄段的朋友

    Some people will make friend requests. The list of their ages is given and ages[i] is the age of the ...

  7. 6-使用requests库封装类处理get/post请求

    1.request安装 1)pip安装,直接pip install requests 2)下载离线包安装,加压后,命令行进入路径,执行python setup.py install 2.创建工程 注意 ...

  8. requests库入门11-重定向和请求历史

    默认情况下,除了head请求,requests会自动处理重定向 重定向就是会把url重新指定到另一个.比如github,使用http会自动重定向到https.一些公司也会使用网关啥的做重定向. r = ...

  9. Python3下requests库发送multipart/form-data类型请求

    [本文出自天外归云的博客园] 要模拟multipart/form-data类型请求,可以用python3的requests库完成.代码示例如下: #请求的接口url url = "url&q ...

随机推荐

  1. [Reproduced] How to Improve Code Quality?

    How to Improve Code Quality? Ref: https://www.perforce.com/blog/sca/what-code-quality-and-how-improv ...

  2. 洛谷P4213(杜教筛)

    #include <bits/stdc++.h> using namespace std; typedef long long LL; const int maxn = 3e6 + 3; ...

  3. 当电脑上有两个mysql时,如何让jmeter连接自己需要的那个mysql

    1.当有两个mysql时,修改其中一个的mysql端口号为3307 ,也可以是其他8788, 在文件my.ini中修改端口号为:3307:修改完之后记得重启mysql哦:dos下命令可以用net st ...

  4. KVM-virsh常用命令

    virsh list #在线VM virsh list --all #所有VM virsh start #开机 virsh shutdown #软关机 virsh destroy #强制关机 virs ...

  5. 删除WordPress菜单wp-nav-menu中li的class或id样式

    我们都知道wordpress已经集成了一些通用的css样式,比如wp-nav-menu菜单会有很多的class,不想看到那么多的选择器,想要清净的世界要如何操作呢?随ytkah一起来看看 <li ...

  6. crc计算工具推荐

    比较好的crc计算工具,32位64位系统都可以用的.crc的校验方法也很多.推荐使用 下载地址

  7. janusgraph-控制台操作命令

    当顶点数量过多时(我的230w)删除太慢 就用下面的命令, 删除整个图库 graph.close() JanusGraphFactory.drop(graph) 查询所有的顶点属性 用traversa ...

  8. SQL SERVER错误:已超过了锁请求超时时段。

    问题:远程连接数据库,无法打开视图,报错:SQL SERVER错误:已超过了锁请求超时时段. (Microsoft SQL Server,错误: 1222) 执行语句获取进程id select * f ...

  9. Jmeter+ant+jekins环境配置

    Jmeter+ant+jekins 一.ant安装 1. ant安装 官网下载http://ant.apache.org 解压到想要的盘里面 2. 配置环境变量 (1)变量名:ANT_HOME 变量值 ...

  10. C Primer Plus--结构和其他数据类型(2)

    目录 枚举类型 enumerated type 枚举默认值 为枚举指定值 命名空间 namespace typedef关键字 * () []修饰符 typedef与这三个运算符结合 函数与指针 函数指 ...