LeetCode 578. Get Highest Answer Rate Question

Get the highest answer rate question from a table survey_log with these columns: uidactionquestion_idanswer_idq_numtimestamp.

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.

Example:

Input:
+------+-----------+--------------+------------+-----------+------------+
| uid  | action    | question_id  | answer_id  | q_num     | timestamp  |
+------+-----------+--------------+------------+-----------+------------+
| 5    | show      | 285          | null       | 1         | 123        |
| 5    | answer    | 285          | 124124     | 1         | 124        |
| 5    | show      | 369          | null       | 2         | 125        |
| 5    | skip      | 369          | null       | 2         | 126        |
+------+-----------+--------------+------------+-----------+------------+
Output:
+-------------+
| survey_log  |
+-------------+
|    285      |
+-------------+
Explanation:
question 285 has answer rate 1/1, while question 369 has 0/1 answer rate, so output 285.

 

Note: The highest answer rate meaning is: answer number's ratio in show number in the same question.

 

drop table survey_log
create table survey_log (uid int,action varchar(25),question_id int,answer_id int,q_num int,timestamp int)

insert into survey_log values(5,'show',285,null,1,123)
insert into survey_log values(5,'answer',285,124124,1,124)
insert into survey_log values(5,'show',369,null,2,125)
insert into survey_log values(5,'skip',369,null,2,126)

select * from survey_log

with cte1 as(
select question_id,count(*) as answer_cnt from survey_log
where action='answer'
group by question_id
),cte2 as(
select question_id,count(*) as question_cnt from survey_log
where action='show'
group by question_id
)select top 1 c1.question_id
from cte1 c1 join cte2  c2 on c1.question_id=c2.question_id
order by (answer_cnt/question_cnt) desc

select top 1 question_id from 
(
select question_id,
sum(case when action='answer' then 1 else 0 end) as answer_cnt,
sum(case when action='show' then 1 else 0 end) as question_cnt
from survey_log
group by question_id
) as tpl
order by answer_cnt/question_cnt desc

#seems it is incorrect answer

select top 1 question_id
from survey_log
group by question_id
order by count(answer_id)/sum(case when action='show' then 1 else 0 end) desc

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值