597. Friend Requests I: Overall Acceptance Rate

https://leetcode.com/articles/friend-requests-i-overall-acceptance-rate/

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.

 

Method 1:

select

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

 

Method 2:

select round(

ifnull(acc/cast(req as float),0),

2) accept_rate from

select count(*) req   from

(

select distinct sender_id, send_to_id from friend_request

)) r,

(select count(*) acc from 

(

select distinct requester_id, accepter_id from request_accepted

)) a

posted @ 2018-10-22 10:34  ffeng0312  阅读(300)  评论(0编辑  收藏  举报