-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1934_Confirmation_Rate.sql
66 lines (54 loc) · 3.09 KB
/
1934_Confirmation_Rate.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
-- Source: 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 primary key 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 for this table.
-- user_id is a foreign key with a reference to the Signups table.
-- action is an ENUM 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 an SQL query to find the confirmation rate of each user.
-- Return the result table in any order.
------------------------------------------------------------------------------
-- SQL Schema
Create table If Not Exists Signups (user_id int, time_stamp datetime)
Create table If Not Exists Confirmations (user_id int, time_stamp datetime, action ENUM('confirmed','timeout'))
Truncate table Signups
insert into Signups (user_id, time_stamp) values ('3', '2020-03-21 10:16:13')
insert into Signups (user_id, time_stamp) values ('7', '2020-01-04 13:57:59')
insert into Signups (user_id, time_stamp) values ('2', '2020-07-29 23:09:44')
insert into Signups (user_id, time_stamp) values ('6', '2020-12-09 10:39:37')
Truncate table Confirmations
insert into Confirmations (user_id, time_stamp, action) values ('3', '2021-01-06 03:30:46', 'timeout')
insert into Confirmations (user_id, time_stamp, action) values ('3', '2021-07-14 14:00:00', 'timeout')
insert into Confirmations (user_id, time_stamp, action) values ('7', '2021-06-12 11:57:29', 'confirmed')
insert into Confirmations (user_id, time_stamp, action) values ('7', '2021-06-13 12:58:28', 'confirmed')
insert into Confirmations (user_id, time_stamp, action) values ('7', '2021-06-14 13:59:27', 'confirmed')
insert into Confirmations (user_id, time_stamp, action) values ('2', '2021-01-22 00:00:00', 'confirmed')
insert into Confirmations (user_id, time_stamp, action) values ('2', '2021-02-28 23:59:59', 'timeout')
-- MS SQL Server Code
SELECT
s.user_id,
ROUND(SUM(CASE
WHEN c.action = 'confirmed' THEN 1.00
WHEN s.time_stamp IS NULL THEN 0.00
ELSE 0.00
END) / COUNT(*), 2) AS confirmation_rate
FROM Signups s
LEFT JOIN Confirmations c
ON s.user_id = c.user_id
GROUP BY s.user_id