578. 查询回答率最高的问题

即一个问题show之后,可能是被answer了,也可能是被skip了,所以求回答率。
其实思路都是一致的,求出回答次数/出现次数即可
题解一

SELECT question_id as survey_log
FROM
(
SELECT question_id,
       SUM(CASE WHEN action = 'answer' THEN 1 ELSE 0 END) AS num_answer,
       SUM(CASE WHEN action = 'show' THEN 1 ELSE 0 END) AS num_show  
	   FROM survey_log
	   GROUP BY question_id
) AS n
ORDER BY (num_answer / num_show) DESC
LIMIT 1

题解二

SELECT question_id AS survey_log
FROM survey_log
GROUP BY question_id
ORDER BY COUNT(answer_id) / COUNT(IF(action = 'show', 1, 0)) DESC
LIMIT 1

你可能感兴趣的:(SQL学习)