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

相关推荐
RainbowSea3 小时前
12. LangChain4j + 向量数据库操作详细说明
java·langchain·ai编程
RainbowSea3 小时前
11. LangChain4j + Tools(Function Calling)的使用详细说明
java·langchain·ai编程
考虑考虑7 小时前
Jpa使用union all
java·spring boot·后端
用户3721574261358 小时前
Java 实现 Excel 与 TXT 文本高效互转
java
RestCloud8 小时前
SQL Server到Hive:批处理ETL性能提升30%的实战经验
数据库·api
阿里云大数据AI技术8 小时前
StarRocks 助力数禾科技构建实时数仓:从数据孤岛到智能决策
大数据
RestCloud8 小时前
为什么说零代码 ETL 是未来趋势?
数据库·api
浮游本尊9 小时前
Java学习第22天 - 云原生与容器化
java
聚客AI10 小时前
🙋‍♀️Transformer训练与推理全流程:从输入处理到输出生成
人工智能·算法·llm
ClouGence10 小时前
CloudCanal + Paimon + SelectDB 从 0 到 1 构建实时湖仓
数据库