Get the highest answer rate question from a tablesurvey_logwith these columns:uid, action, question_id, answer_id, q_num, timestamp.
uid means user id; action has these kind of values: "show", "answer", "skip"; answer_id is not null when action column is "answer", while is null for "show" and "skip"; q_num is the numeral order of the question in current session.
Write a sql query to identify the question which has the highest answer rate.
Note:The highest answer rate meaning is: answer number's ratio in show number in the same question.
Solution 1
SELECT question_id AS survey_logFROM ( SELECT question_id, SUM(IF(action ='show',1,0)) AS show_num, SUM(IF(action ='answer',1,0)) AS answer_num FROM survey_log GROUP BY question_id) AS tempORDER BY IFNULL((answer_num / show_num),1) DESC, answer_num DESCLIMIT 1;
Solution 2
SELECT question_id as survey_logFROM survey_logGROUP BY question_idORDER BY avg(case when action ='answer' then 1else0 end) descLIMIT 1;