【LeetCode-SQL】578. 查询回答率最高的问题
2022/3/1 19:21:43
本文主要是介绍【LeetCode-SQL】578. 查询回答率最高的问题,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
目录
- 一、题目
- 二、解决
- 1、sum() 后子查询
- 2、join
- 3、avg() 排序
- 4、针对解
- 三、参考
一、题目
SurveyLog 表:
+-------------+------+ | Column Name | Type | +-------------+------+ | id | int | | action | ENUM | | question_id | int | | answer_id | int | | q_num | int | | timestamp | int | +-------------+------+ 这张表没有主键,其中可能包含重复项。 action 是一个 ENUM 数据,可以是 "show"、"answer" 或者 "skip" 。 这张表的每一行表示:ID = id 的用户对 question_id 的问题在 timestamp 时间进行了 action 操作。 如果用户对应的操作是 "answer" ,answer_id 将会是对应答案的 id ,否则,值为 null 。 q_num 是该问题在当前会话中的数字顺序。
回答率 是指:同一问题编号中回答次数占显示次数的比率。
编写一个 SQL 查询以报告 回答率 最高的问题。如果有多个问题具有相同的最大 回答率 ,返回 question_id 最小的那个。
查询结果如下例所示。
示例:
输入: SurveyLog table: +----+--------+-------------+-----------+-------+-----------+ | id | action | question_id | answer_id | q_num | timestamp | +----+--------+-------------+-----------+-------+-----------+ | 5 | show | 285 | null | 1 | 123 | | 5 | answer | 285 | 124124 | 1 | 124 | | 5 | show | 369 | null | 2 | 125 | | 5 | skip | 369 | null | 2 | 126 | +----+--------+-------------+-----------+-------+-----------+ 输出: +------------+ | survey_log | +------------+ | 285 | +------------+ 解释: 问题 285 显示 1 次、回答 1 次。回答率为 1.0 。 问题 369 显示 1 次、回答 0 次。回答率为 0.0 。 问题 285 回答率最高。
二、解决
1、sum() 后子查询
思路:
通过 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、join
思路:
S1:先查出 action = answer 的数据存为一张临时表 action_answer;
S2:再查出 action = show 的数据作为一张临时表 show_answer;
S3:通过 question_id 连接两表;
S4:使用 order by 对回答进行排列,取第一条数据。
代码:
select action_answer.question_id as survey_log from ( select question_id, count(*) as answer_count from surveyLog where action = 'answer' group by question_id ) as action_answer join ( select question_id, count(*) as show_count from surveyLog where action = 'show' group by question_id ) as action_show using(question_id) order by answer_count / show_count desc limit 1;
3、avg() 排序
思路:
通过计算每道题的 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;
4、针对解
思路:
一道题只有回答了才有 answer_id ,所以answer_id 数量最多的就是回答率最高的。
代码:
select question_id as survey_log from surveyLog group by question_id order by count(answer_id) desc limit 1;
三、参考
1、四种方法解查询回答率最高的问题
这篇关于【LeetCode-SQL】578. 查询回答率最高的问题的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-04-26高性能表格工具VTable总体构成-icode9专业技术文章分享
- 2024-04-16软路由代理问题, tg 无法代理问题-icode9专业技术文章分享
- 2024-04-16程序猿用什么锅-icode9专业技术文章分享
- 2024-04-16自建 NAS 的方案-icode9专业技术文章分享
- 2024-04-14ansible 在远程主机上执行脚本,并传入参数-icode9专业技术文章分享
- 2024-04-14ansible 在远程主机上执行脚本,并传入参数, 加上remote_src: yes 配置-icode9专业技术文章分享
- 2024-04-14ansible 检测远程主机的8080端口,如果关闭,则echo 进程已关闭-icode9专业技术文章分享
- 2024-04-14result 成功怎么写-icode9专业技术文章分享
- 2024-04-14stopped 状态设置为变量,由外部传递进来-icode9专业技术文章分享
- 2024-04-14为什么ansible执行远程脚本需要放到后台-icode9专业技术文章分享