mysql - MySQL:与dense_rank()over()的group by和partition的差异输出?
问题描述
我在 Leetcode 上做一个 MySQL 问题。(链接:https ://leetcode.com/problems/get-highest-answer-rate-question/ )问题是找到最大值。我使用order by + limit 1来得到答案。但是如果有多个最大值呢?限制 1只会返回 1 个答案。
我尝试使用dense_rank() 来解决这个问题,但是当我使用partition by 和group by 时,我发现输出是不同的。
Input: {"headers": {"survey_log": ["id", "action", "question_id", "answer_id", "q_num", "timestamp"]},"rows": {"survey_log": [[5, "show", 285, null, 1, 123], [5, "answer", 285, 124124, 1, 124], [5, "show", 369, null, 2, 125], [5, "skip", 369, null, 2, 126]]}}
如果我的代码是:
# Method 1
select question_id,
dense_rank() over (order by count(case when action = 'answer' then 1 end)/
count(case when action = 'show' then 1 end) desc) as num
from survey_log
group by question_id
然后我得到输出:
Output: {"headers": ["question_id", "num"], "values": [[285, 1], [369, 2]]}
但是,当我尝试使用 partition by 来实现相同的效果时,输出不是我想要的:
# Method 2
select question_id,
dense_rank() over (partition by question_id
order by count(case when action = 'answer' then 1 end)/
count(case when action = 'show' then 1 end) desc) as num
from survey_log
Output: {"headers": ["question_id", "num"], "values": [[285, 1]]}
我不知道为什么这里的输出不同。谁能解释一下?提前致谢!!
更新:对不起,我没有清楚地说明这个问题。问题是“编写一个 sql 查询来识别具有最高回答率的问题”。
“最高答题率的意思是:同一题中答题数占节目数的比例。”
至于上面的输入,第285题的回答率为1/1,而第369题的回答率为0/1,所以输出285。那么输出应该是:285 输出
我的困惑是为什么方法 2 的输出与方法 1 不同?谢谢!!
解决方案
我将从计算每个问题的回答率的查询开始。根据您的问题陈述,应该是:
select
question_id,
sum(action = 'answer') / nullif(sum(action = 'show'), 0) answer_rate
from survey_log
group by question_id
您可以使用该信息对问题进行排名。您希望针对所有其他组对每个问题进行排名,因此窗口函数不应包含partition
子句:
select
question_id,
rank() over(order by sum(action = 'answer') / nullif(sum(action = 'show'), 0) desc) rn
from survey_log
group by question_id
order by rn
推荐阅读
- android - 检查 GitHub 中的 Android 项目信息
- reactjs - React Native Invariant Violation "Main" 尚未向 react-native-track-player 注册
- javascript - 用负数或数字范围拆分连字符分隔的字符串
- python - 有没有办法根据交替元素将数据框中的排序值分配给组
- node.js - 使用 jwt 的护照谷歌策略
- javascript - 在其他播放器完成初始化后,videoJs 播放器无法正确初始化
- c# - “元素已经是另一个元素的逻辑子元素”即使在清除项目集合时也会出现异常
- java - 比较两个句子并检查它们是否有相似的单词
- vue.js - Vue:重构计算属性
- javascript - 在 DJango 模板中创建一个迭代计数器