Skip to content

Commit 45d3c02

Browse files
[N-0] add 578
1 parent 04a0829 commit 45d3c02

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,7 @@ Your ideas/fixes/algorithms are more than welcome!
685685
|585|[Investments in 2016](https://leetcode.com/problems/investments-in-2016/)|[Solution](../master/database/_585.java) | || Medium|
686686
|584|[Find Customer Referee](https://leetcode.com/problems/find-customer-referee/)|[Solution](../master/database/_584.java) | || Easy|
687687
|580|[Count Student Number in Departments](https://leetcode.com/problems/count-student-number-in-departments/)|[Solution](../master/database/_580.sql) | || Medium | Left Join
688+
|578|[Get Highest Answer Rate Question](https://leetcode.com/problems/get-highest-answer-rate-question/)|[Solution](../master/database/_578.sql) | || Medium |
688689
|577|[Employee Bonus](https://leetcode.com/problems/employee-bonus/)|[Solution](../master/database/_577.sql) | || Easy |
689690
|574|[Winning Candidate](https://leetcode.com/problems/winning-candidate/)|[Solution](../master/database/_574.sql) | || Medium |
690691
|571|[Find Median Given Frequency of Numbers](https://leetcode.com/problems/find-median-given-frequency-of-numbers/)|[Solution](../master/database/_571.sql) | || Hard |

database/_578.sql

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--578. Get Highest Answer Rate Question
2+
--
3+
--Get the highest answer rate question from a table survey_log with these columns: uid, action, question_id, answer_id, q_num, timestamp.
4+
--
5+
--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.
6+
--
7+
--Write a sql query to identify the question which has the highest answer rate.
8+
--
9+
--Example:
10+
--Input:
11+
--+------+-----------+--------------+------------+-----------+------------+
12+
--| uid | action | question_id | answer_id | q_num | timestamp |
13+
--+------+-----------+--------------+------------+-----------+------------+
14+
--| 5 | show | 285 | null | 1 | 123 |
15+
--| 5 | answer | 285 | 124124 | 1 | 124 |
16+
--| 5 | show | 369 | null | 2 | 125 |
17+
--| 5 | skip | 369 | null | 2 | 126 |
18+
--+------+-----------+--------------+------------+-----------+------------+
19+
--Output:
20+
--+-------------+
21+
--| survey_log |
22+
--+-------------+
23+
--| 285 |
24+
--+-------------+
25+
--Explanation:
26+
--question 285 has answer rate 1/1, while question 369 has 0/1 answer rate, so output 285.
27+
--Note: The highest answer rate meaning is: answer number's ratio in show number in the same question.
28+
29+
SELECT question_id AS 'survey_log' FROM survey_log GROUP BY question_id ORDER BY
30+
COUNT(answer_id) / COUNT(case when survey_log.action =
31+
'show' then survey_log.action else null end) DESC LIMIT 0,1

0 commit comments

Comments
 (0)