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

相关推荐
五岳40 分钟前
分库分表数据源ShardingSphereDataSource的Connection元数据误用问题分析
java·mysql·爬坑
带刺的坐椅40 分钟前
迈向 MCP 集群化:Solon AI (支持 Java8+)在解决 MCP 服务可扩展性上的探索与实践
java·ai·llm·solon·mcp
悄悄敲敲敲1 小时前
MySQL表的约束
数据库·mysql
鼠爷ねずみ1 小时前
SpringCloud前后端整体开发流程-以及技术总结文章实时更新中
java·数据库·后端·spring·spring cloud
代码or搬砖1 小时前
String字符串
android·java·开发语言
九皇叔叔2 小时前
MySQL 数据库 Read View 详解
数据库·mysql·mvcc·read view
leo__5202 小时前
基于两步成像算法的聚束模式SAR MATLAB实现
开发语言·算法·matlab
字节数据平台2 小时前
评测也很酷,Data Agent 自动化评测的三层框架与实战
大数据
前端小白在前进2 小时前
力扣刷题:在排序数组中查找元素的第一个和最后一个位置
数据结构·算法·leetcode