+----------------+----------+
|Column Name |Type|+----------------+----------+
| user_id | int || time_stamp | datetime |+----------------+----------+
user_id is the columnofuniquevaluesfor this table.
Eachrowcontains information about the signup time for the userwith ID user_id.
Table: Confirmations
1
2
3
4
5
6
7
8
9
10
+----------------+----------+
|Column Name |Type|+----------------+----------+
| user_id | int || time_stamp | datetime || action | ENUM |+----------------+----------+
(user_id, time_stamp) is the primarykey (combination of columns withuniquevalues) for this table.
user_id is a foreignkey (reference column) to the Signups table.
action is an ENUM (category) of the type ('confirmed', 'timeout') Eachrowof this table indicates that the userwith 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.
Input:
Signups table:+---------+---------------------+| user_id | time_stamp |+---------+---------------------+|3|2020-03-2110:16:13||7|2020-01-0413:57:59||2|2020-07-2923:09:44||6|2020-12-0910:39:37|+---------+---------------------+Confirmations table:+---------+---------------------+-----------+| user_id | time_stamp | action |+---------+---------------------+-----------+|3|2021-01-0603:30:46| timeout ||3|2021-07-1414:00:00| timeout ||7|2021-06-1211:57:29| confirmed ||7|2021-06-1312:58:28| confirmed ||7|2021-06-1413:59:27| confirmed ||2|2021-01-2200:00:00| confirmed ||2|2021-02-2823:59:59| timeout |+---------+---------------------+-----------+Output:
+---------+-------------------+| user_id | confirmation_rate |+---------+-------------------+|6|0.00||3|0.00||7|1.00||2|0.50|+---------+-------------------+Explanation:
User 6 did not request any confirmation messages. The confirmation rate is0.User 3 made 2 requests and both timed out. The confirmation rate is0.User 7 made 3 requests and all were confirmed. The confirmation rate is1.User 2 made 2 requests where one was confirmed and the other timed out. The confirmation rate is1/2=0.5.
We need to compute the confirmation rate for each user, defined as the number of confirmed actions divided by the number of signups. This is a classic aggregation and join problem.
SELECT s.user_id,
IFNULL(ROUND(COUNT(c.action) /1, 2), 0) AS confirmation_rate
FROM Signups s
LEFTJOIN Confirmations c
ON s.user_id = c.user_id AND c.action ='confirmed'GROUPBY s.user_id
ORDERBY s.user_id;
1
2
3
4
5
6
7
SELECT s.user_id,
COALESCE(ROUND(COUNT(c.action)::numeric /1, 2), 0) AS confirmation_rate
FROM Signups s
LEFTJOIN Confirmations c
ON s.user_id = c.user_id AND c.action='confirmed'GROUPBY s.user_id
ORDERBY s.user_id;