Skip to content

Commit 384af65

Browse files
authored
MEDIUM
1 parent 473dec2 commit 384af65

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

1934. Confirmation Rate.sql

+84
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,87 @@
1+
-- Problem:
2+
3+
/*
4+
Table: Signups
5+
6+
+----------------+----------+
7+
| Column Name | Type |
8+
+----------------+----------+
9+
| user_id | int |
10+
| time_stamp | datetime |
11+
+----------------+----------+
12+
user_id is the column of unique values for this table.
13+
Each row contains information about the signup time for the user with ID user_id.
14+
15+
16+
Table: Confirmations
17+
18+
+----------------+----------+
19+
| Column Name | Type |
20+
+----------------+----------+
21+
| user_id | int |
22+
| time_stamp | datetime |
23+
| action | ENUM |
24+
+----------------+----------+
25+
(user_id, time_stamp) is the primary key (combination of columns with unique values) for this table.
26+
user_id is a foreign key (reference column) to the Signups table.
27+
action is an ENUM (category) of the type ('confirmed', 'timeout')
28+
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').
29+
30+
31+
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.
32+
33+
Write a solution to find the confirmation rate of each user.
34+
35+
Return the result table in any order.
36+
37+
The result format is in the following example.
38+
39+
40+
41+
Example 1:
42+
43+
Input:
44+
Signups table:
45+
+---------+---------------------+
46+
| user_id | time_stamp |
47+
+---------+---------------------+
48+
| 3 | 2020-03-21 10:16:13 |
49+
| 7 | 2020-01-04 13:57:59 |
50+
| 2 | 2020-07-29 23:09:44 |
51+
| 6 | 2020-12-09 10:39:37 |
52+
+---------+---------------------+
53+
Confirmations table:
54+
+---------+---------------------+-----------+
55+
| user_id | time_stamp | action |
56+
+---------+---------------------+-----------+
57+
| 3 | 2021-01-06 03:30:46 | timeout |
58+
| 3 | 2021-07-14 14:00:00 | timeout |
59+
| 7 | 2021-06-12 11:57:29 | confirmed |
60+
| 7 | 2021-06-13 12:58:28 | confirmed |
61+
| 7 | 2021-06-14 13:59:27 | confirmed |
62+
| 2 | 2021-01-22 00:00:00 | confirmed |
63+
| 2 | 2021-02-28 23:59:59 | timeout |
64+
+---------+---------------------+-----------+
65+
Output:
66+
+---------+-------------------+
67+
| user_id | confirmation_rate |
68+
+---------+-------------------+
69+
| 6 | 0.00 |
70+
| 3 | 0.00 |
71+
| 7 | 1.00 |
72+
| 2 | 0.50 |
73+
+---------+-------------------+
74+
Explanation:
75+
User 6 did not request any confirmation messages. The confirmation rate is 0.
76+
User 3 made 2 requests and both timed out. The confirmation rate is 0.
77+
User 7 made 3 requests and all were confirmed. The confirmation rate is 1.
78+
User 2 made 2 requests where one was confirmed and the other timed out. The confirmation rate is 1 / 2 = 0.5.
79+
*/
80+
81+
-------------------------------------------------------------------------------
82+
83+
-- Solution:
84+
185
SELECT s.user_id , ROUND(IFNULL(sum(ACTION='confirmed')/COUNT(*),0),2) AS confirmation_rate
286
FROM Signups s
387
LEFT JOIN Confirmations c ON s.user_id = c.user_id

0 commit comments

Comments
 (0)