0
点赞
收藏
分享

微信扫一扫

LeetCode(数据库)- 查询回答率最高的问题


题目链接:​​点击打开链接​​


题目大意:略。


解题思路:略。


AC 代码

-- 解决方案(1)
WITH t1 AS(
SELECT question_id, IF(action = 'show', 1, 0) shown, IF(action = 'answer', 1, 0) answern, IF(action = 'skip', 1, 0) skipn
FROM survey_log
),

t2 AS(
SELECT question_id, SUM(answern) OVER(PARTITION BY question_id) sum_answer, SUM(shown) OVER(PARTITION BY question_id) sum_show
FROM t1
),

t3 AS(
SELECT question_id, sum_answer/sum_show rate
FROM t2
)

SELECT question_id survey_log
FROM t3
WHERE question_id IS NOT NULL
ORDER BY rate DESC
LIMIT 1

-- 解决方案(2)
select AnswerCnt.question_id as survey_log from
(select question_id, count(*) as answer_cnt
from survey_log
where action = "answer"
group by question_id) as AnswerCnt
join
(select question_id, count(*) as action_cnt
from survey_log
where action = "show"
group by question_id) as ShowCnt
on AnswerCnt.question_id = ShowCnt.question_id
order by AnswerCnt.answer_cnt / ShowCnt.action_cnt desc
limit 1;

-- 解决方案(3)
select question_id as survey_log
from (
select
question_id,
sum(if(action = 'answer', 1, 0)) as AnswerCnt,
sum(if(action = 'show', 1, 0)) as ShowCnt
from
survey_log
group by question_id
) as tbl
order by (AnswerCnt / ShowCnt) desc
limit 1;

-- 解决方案(4)
select question_id as survey_log
from survey_log
group by question_id
order by sum(if(action = 'answer', 1, 0)) / sum(if(action = 'show', 1, 0)) desc
limit 1;


举报

相关推荐

数据库查询

0 条评论