You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
select question_id as survey_log from (
select
question_id,
sum(case action when 'answer' then 1 else 0 end) /sum(case action when 'show' then 1 else 0 end) as rate
from surveyLog group by question_id order by rate desc
) as temp limit1;
解析
通过 sum 和 case 计算出回答率 rate ,并且升序排列,作为临时表 temp
查询临时表 temp 取第一条数据
SQL:方法二
selectaction_answer.question_idas survey_log from (
select question_id, count(*) as answer_count from surveyLog
where action ='answer'group by question_id
) as action_answer join (
select question_id, count(*) as show_count from surveyLog
where action ='show'group by question_id
) as action_show using(question_id)
order by answer_count / show_count desclimit1;
解析
首先查出 action = answer 的数据存为一张临时表 action_answer
再查出 action = show 的数据作为一张临时表 show_answer
通过 question_id 连接两表
使用 order by 对回答进行排列,取第一条数据
SQL:方法三
select question_id as survey_log from surveyLog
group by question_id
order byavg(action ='answer') desclimit1;
题目
查询回答率最高的问题,如果回答率相同,返回
question_id
最小的那个。解释:
285
显示1
次,回答1
次,回答率为1.0
369
显示1
次,回答0
次,回答率为0.0
SQL:方法一
解析
sum
和case
计算出回答率rate
,并且升序排列,作为临时表temp
temp
取第一条数据SQL:方法二
解析
action = answer
的数据存为一张临时表action_answer
action = show
的数据作为一张临时表show_answer
question_id
连接两表order by
对回答进行排列,取第一条数据SQL:方法三
解析
一道题只有回答了
action
才是answer
,这里通过计算每道题的action = 'answer'
的平均数,因为这里计算平均数的分数是question_id
的个数,所以action = 'answer'
个数越多,回答率越高,最后取第一条数据。ps:这里默认question_id
是升序排列的SQL:方法四
解析
一道题只有回答了才有
answer_id
,所以计算出每道题有多少个answer_id
,answer_id
数量最多的就是回答率最高的,ps:这里默认question_id
是升序排列的The text was updated successfully, but these errors were encountered: