力扣之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 结果截图

相关推荐
威视锐科技1 小时前
软件定义无线电36
网络·网络协议·算法·fpga开发·架构·信息与通信
牧歌悠悠1 小时前
【Python 算法】动态规划
python·算法·动态规划
JINX的诅咒1 小时前
CORDIC算法:三角函数的硬件加速革命——从数学原理到FPGA实现的超高效计算方案
算法·数学建模·fpga开发·架构·信号处理·硬件加速器
IT成长日记2 小时前
【MySQL基础】聚合函数从基础使用到高级分组过滤
数据库·mysql·聚合函数
明天不下雨(牛客同名)2 小时前
为什么 ThreadLocalMap 的 key 是弱引用 value是强引用
java·jvm·算法
lisw053 小时前
DeepSeek原生稀疏注意力(Native Sparse Attention, NSA)算法介绍
人工智能·深度学习·算法
多多*3 小时前
Java设计模式 简单工厂模式 工厂方法模式 抽象工厂模式 模版工厂模式 模式对比
java·linux·运维·服务器·stm32·单片机·嵌入式硬件
喝拿铁写前端4 小时前
SmartField AI:让每个字段都找到归属!
前端·算法
莫有杯子的龙潭峡谷4 小时前
3.31 代码随想录第三十一天打卡
c++·算法