Skip to content

Commit 38332cc

Browse files
committed
Upload 197
1 parent 23894c5 commit 38332cc

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!-- markdownlint-disable MD041 -->
22
[![eevveerryyddaayy release](https://img.shields.io/badge/eevveerryyddaayy-v1.3.0-blue.svg)](https://github.com/ggeerraarrdd/eevveerryyddaayy/)
3-
[![Solved](https://img.shields.io/badge/solved-196-green.svg)](#index)
3+
[![Solved](https://img.shields.io/badge/solved-197-green.svg)](#index)
44
<!-- markdownlint-enable MD041 -->
55

66
# SQL Everyday
@@ -230,6 +230,7 @@ Because this project necessitated a framework to enable consistent daily practic
230230
| 194 | [Reported Posts](https://leetcode.com/problems/reported-posts/description/) | [Solution](solutions/194_reported_posts.md) | LeetCode | Easy | |
231231
| 195 | [Game Play Analysis III](https://leetcode.com/problems/game-play-analysis-iii/description/) | [Solution](solutions/195_game_play_analysis_iii.md) | LeetCode | Medium | |
232232
| 196 | [Winning Candidate](https://leetcode.com/problems/winning-candidate/description/) | [Solution](solutions/196_winning_candidate.md) | LeetCode | Medium | |
233+
| 197 | [Get Highest Answer Rate Question](https://leetcode.com/problems/get-highest-answer-rate-question/) | [Solution](solutions/197_get_highest_answer_rate_question.md) | LeetCode | Medium | |
233234
<!-- Index End - WARNING: Do not delete or modify this markdown comment. -->
234235
<!--- cSpell:enable --->
235236

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# SQL Everyday \#197
2+
3+
## Get Highest Answer Rate Question
4+
5+
Site: LeetCode\
6+
Difficulty per Site: Medium
7+
8+
## Problem
9+
10+
The **answer rate** for a question is the number of times a user answered the question by the number of times a user showed the question.
11+
12+
Write a solution to report the question that has the highest **answer rate**. If multiple questions have the same maximum **answer rate**, report the question with the smallest `question_id`. [[Full Description](https://leetcode.com/problems/get-highest-answer-rate-question/)]
13+
14+
## Submitted Solution
15+
16+
```sql
17+
-- Submitted Solution
18+
WITH cte1 AS (
19+
SELECT
20+
question_id
21+
,1.00 * SUM(CASE WHEN action = 'answer' THEN 1 ELSE 0 END) /
22+
SUM(CASE WHEN action = 'show' THEN 1 ELSE 0 END) AS rate
23+
FROM SurveyLog
24+
GROUP BY question_id
25+
),
26+
cte2 AS (
27+
SELECT
28+
question_id
29+
,ROW_NUMBER() OVER (ORDER BY rate DESC, question_id ASC) AS rownum
30+
FROM cte1
31+
)
32+
SELECT
33+
question_id AS survey_log
34+
FROM cte2
35+
WHERE rownum = 1
36+
;
37+
```
38+
39+
## Site Solution
40+
41+
```sql
42+
-- LeetCode Solution
43+
-- Approach 1: Getting the Highest and the Smallest Using RANK()
44+
WITH answer_rate AS
45+
(
46+
SELECT question_id,
47+
SUM(CASE WHEN action = 'answer' THEN 1 ELSE 0 END)
48+
/ SUM(CASE WHEN action = 'show' THEN 1 ELSE 0 END) AS rate
49+
FROM surveylog
50+
GROUP BY question_id
51+
)
52+
SELECT question_id AS survey_log
53+
FROM
54+
(
55+
SELECT question_id,
56+
RANK()OVER(ORDER BY rate DESC question_id) AS rnk
57+
FROM answer_rate
58+
) AS t0
59+
WHERE rnk = 1
60+
61+
-- Approach 2: Getting the Highest and the Smallest Using ORDER BY + LIMIT
62+
SELECT question_id AS survey_log
63+
FROM surveylog
64+
GROUP BY question_id
65+
ORDER BY SUM(CASE WHEN action = 'answer' THEN 1 ELSE 0 END)
66+
/ SUM(CASE WHEN action = 'show' THEN 1 ELSE 0 END) DESC
67+
, question_id ASC
68+
LIMIT 1
69+
```
70+
71+
## Notes
72+
73+
TBD
74+
75+
## NB
76+
77+
TBD
78+
79+
Go to [Index](../?tab=readme-ov-file#index)\
80+
Go to [Overview](../?tab=readme-ov-file)

0 commit comments

Comments
 (0)