算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !
今天和大家聊的问题叫做 查询回答率最高的问题,我们先来看题面:
https://leetcode-cn.com/problems/get-highest-answer-rate-question/
解题
解法1:通过 sum 和 case 计算出回答率 rate ,并且升序排列,作为临时表 temp,然后查询 temp 取第一条数据。
select question_id as survey_log from (
select
question_id,
sum(case action when 'answer' then 1 else 0 end) /
sum(case action when 'show' then 1 else 0 end) as rate
from surveyLog group by question_id order by rate desc
) as temp limit 1;
解法2:通过计算每道题的 action = ‘answer’ 的平均数来确定最多回答问题的question_id。因为 action = ‘answer’ 个数越多,回答率越高,计算是 question_id 平均数分数,最后取第一条数据。
select question_id as survey_log from surveyLog
group by question_id
order by avg(action = 'answer') desc limit 1;
好了,今天的文章就到这里,如果觉得有所收获,请顺手点个在看或者转发吧,你们的支持是我最大的动力 。
上期推文: