-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfirmationRate.sql
More file actions
40 lines (35 loc) · 869 Bytes
/
ConfirmationRate.sql
File metadata and controls
40 lines (35 loc) · 869 Bytes
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
--- Example Problem: Confirmation Rate
--- The confirmation rate of a user is the number of 'confirmed' messages divided by the total number of requested confirmation messages. Write a solution to find the confirmation rate of each user.
--- Return the result table in any order.
-- Solution (Using PL/SQL)
/*
Table Structures where as followed:
Signups Table:
user_id int
time_stamp datetime
Confirmation Table:
user_id int
time_stamp datetime
action ENUM
*/
WITH userActions AS (
SELECT
signups.user_id, confirmations.action
from signups
LEFT OUTER JOIN
confirmations on signups.user_id = confirmations.user_id
)
SELECT
counts.user_id,
round(avg(confirmations),2) as confirmation_rate
FROM (
SELECT
userActions.user_id,
CASE
WHEN userActions.action = 'confirmed' then 1
ELSE 0
END AS confirmations
FROM
userActions
) counts
GROUP BY counts.user_id