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

相关推荐
焦糖布丁的午夜20 分钟前
MySQL数据库大王小练习
数据库·mysql
windfantasy199028 分钟前
编程考级体系:覆盖全学段多科目的优势分析
大数据
AI视觉网奇1 小时前
Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr
开发语言·c++·算法
ghie90901 小时前
ECG波形检查与分析系统
算法
智者知已应修善业1 小时前
【输入两个数字,判断两数相乘是否等于各自逆序数相乘】2023-10-24
c语言·c++·经验分享·笔记·算法·1024程序员节
Tony Bai1 小时前
Go 安全新提案:runtime/secret 能否终结密钥残留的噩梦?
java·开发语言·jvm·安全·golang
oioihoii1 小时前
C++11到C++23语法糖万字详解
java·c++·c++23
pengzhuofan1 小时前
Java演进与与工程师成长
java·开发语言
月明长歌1 小时前
再谈Java 继承与多态:从“能用”到“精通”,更深一层的原理与设计思维
java·开发语言
狗头实习生1 小时前
Spring常见的事务失效原因
java·数据库·spring