力扣之578.查询回答率最高的问题

文章目录

  • [1. 578.查询回答率最高的问题](#1. 578.查询回答率最高的问题)
    • [1.1 题干](#1.1 题干)
    • [1.2 准备数据](#1.2 准备数据)
    • [1.3 解法](#1.3 解法)
    • [1.4 结果截图](#1.4 结果截图)

1. 578.查询回答率最高的问题

1.1 题干

SurveyLog 表:

±------------±-----+

| Column Name | Type |

±------------±-----+

| id | int |

| action | ENUM |

| question_id | int |

| answer_id | int |

| q_num | int |

| timestamp | int |

±------------±-----+

这张表可能包含重复项。

action 是一个 ENUM(category) 数据,可以是 "show"、"answer" 或者 "skip" 。

这张表的每一行表示:ID = id 的用户对 question_id 的问题在 timestamp 时间进行了 action 操作。

如果用户对应的操作是 "answer" ,answer_id 将会是对应答案的 id ,否则,值为 null 。

q_num 是该问题在当前会话中的数字顺序。

回答率 是指:同一问题编号中回答次数占显示次数的比率。

编写一个解决方案以报告 回答率 最高的问题。如果有多个问题具有相同的最大 回答率 ,返回 question_id 最小的那个。

查询结果如下例所示。

示例 1:

输入:

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.2 准备数据

sql 复制代码
Create table If Not Exists SurveyLog (id int, action varchar(255), question_id int, answer_id int, q_num int, timestamp int)
Truncate table SurveyLog
insert into SurveyLog (id, action, question_id, answer_id, q_num, timestamp) values ('5', 'show', '285', NULL, '1', '123')
insert into SurveyLog (id, action, question_id, answer_id, q_num, timestamp) values ('5', 'answer', '285', '124124', '1', '124')
insert into SurveyLog (id, action, question_id, answer_id, q_num, timestamp) values ('5', 'show', '369', NULL, '2', '125')
insert into SurveyLog (id, action, question_id, answer_id, q_num, timestamp) values ('5', 'skip', '369', NULL, '2', '126')

1.3 解法

sql 复制代码
with t1 as(
    select distinct question_id,
                sum(if(action='answer',1,0)) over(partition by question_id)/sum(if(action='show',1,0)) over(partition by question_id) hdl
from SurveyLog
)
select question_id survey_log from t1 order by hdl desc,question_id asc limit 1

1.4 结果截图

相关推荐
Warren982 小时前
Vue2博客项目笔记(第一天)
java·开发语言·javascript·vue.js·windows·笔记·ecmascript
Charles Wesley2 小时前
11:java学习笔记:1D array(1维数组)
java·笔记·学习·eclipse
এ᭄画画的北北2 小时前
力扣-283.移动零
算法·leetcode
stillaliveQEJ3 小时前
【JavaEE】多线程(一)
java·java-ee
present--013 小时前
【JAVA EE初阶】多线程(下)
java·java-ee
码明4 小时前
42.MySQL视图
数据库·oracle
山茶花开时。4 小时前
[Oracle] TO_NUMBER()函数
数据库·oracle
2501_924879365 小时前
口罩识别场景误报率↓79%:陌讯多模态融合算法实战解析
人工智能·深度学习·算法·目标检测·智慧城市
Chan165 小时前
【智能协同云图库】第七期:基于AI调用阿里云百炼大模型,实现AI图片编辑功能
java·人工智能·spring boot·后端·spring·ai·ai作画
耳东哇5 小时前
阿里云百炼平台创建智能体-上传文档
数据库·redis·阿里云