Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ Have a good contributing!
- [1193. Monthly Transactions I](./leetcode/medium/1193.%20Monthly%20Transactions%20I.sql)
- [1341. Movie Rating](./leetcode/medium/1341.%20Movie%20Rating.sql)
- [1907. Count Salary Categories](./leetcode/medium/1907.%20Count%20Salary%20Categories.sql)
- [1934. Confirmation Rate](./leetcode/medium/1934.%20Confirmation%20Rate.sql)
3. [Hard](./leetcode/hard/)
- [185. Department Top Three Salaries](./leetcode/hard/185.%20Department%20Top%20Three%20Salaries.sql)

Expand Down
54 changes: 54 additions & 0 deletions leetcode/medium/1934. Confirmation Rate.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Question 1934. Confirmation Rate
Link: https://leetcode.com/problems/confirmation-rate/description/?envType=study-plan-v2&envId=top-sql-50

Table: Signups

+----------------+----------+
| Column Name | Type |
+----------------+----------+
| user_id | int |
| time_stamp | datetime |
+----------------+----------+
user_id is the column of unique values for this table.
Each row contains information about the signup time for the user with ID user_id.


Table: Confirmations

+----------------+----------+
| Column Name | Type |
+----------------+----------+
| user_id | int |
| time_stamp | datetime |
| action | ENUM |
+----------------+----------+
(user_id, time_stamp) is the primary key (combination of columns with unique values) for this table.
user_id is a foreign key (reference column) to the Signups table.
action is an ENUM (category) of the type ('confirmed', 'timeout')
Each row of this table indicates that the user with ID user_id requested a confirmation message at time_stamp and that confirmation message was either confirmed ('confirmed') or expired without confirming ('timeout').


The confirmation rate of a user is the number of 'confirmed' messages divided by the total number of requested confirmation messages. The confirmation rate of a user that did not request any confirmation messages is 0. Round the confirmation rate to two decimal places.

Write a solution to find the confirmation rate of each user.

Return the result table in any order.
*/

SELECT
s.user_id,
COALESCE(CASE
WHEN c.user_id IS NULL THEN 0.00
ELSE ROUND((
SELECT COUNT(c2.user_id)
FROM Confirmations AS c2
WHERE c2.action = 'confirmed' AND c2.user_id = s.user_id
GROUP BY c2.user_id
) / COUNT(c.user_id)::numeric, 2)
END, 0.00) AS confirmation_rate
FROM Signups AS s
LEFT JOIN
Confirmations AS c
ON s.user_id = c.user_id
GROUP BY s.user_id, c.user_id