问题描述
我正在对Leetcode进行MysqL问题。(链接:https://leetcode.com/problems/get-highest-answer-rate-question/)问题是要找到最大值。我使用 order by + limit 1 来获得答案。但是,如果有多个最大值怎么办? 限制1 仅返回1个答案。
我尝试使用density_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",124124,124],369,2,125],"skip",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",1]]}
我不知道为什么这里的输出不同。谁能解释?预先感谢!
更新:很抱歉,我没有清楚说明问题。问题是“编写一个SQL查询以识别答案率最高的问题。”
“最高答案率的意思是:同一问题中节目编号中答案编号的比率。”
对于上面的输入,问题285的回答率为1/1,而问题369的回答率为0/1,因此输出为285。那么输出应为:285 Output
解决方法
我将从查询开始,该查询计算每个问题的回答率。根据您的问题陈述,应该是:
select
question_id,sum(action = 'answer') / nullif(sum(action = 'show'),0) answer_rate
from survey_log
group by question_id
您可以使用该信息对问题进行排名。您想对所有其他组的每个问题进行排名,因此window函数不应包含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