less than 1 minute read

Problem

problem-1934

Query

SELECT 
    -- *,
    -- SUM(IF(c.action = 'confirmed', 1, 0)) as num_of_confirmed,
    -- COUNT(s.user_id) as total_request,
    s.user_id,
    (ROUND(SUM(IF(c.action = 'confirmed', 1, 0)) / COUNT(s.user_id), 2)) AS confirmation_rate
FROM
    Signups AS s
LEFT JOIN
    Confirmations AS c
    ON 
        s.user_id = c.user_id
GROUP BY
    s.user_id

Other Approach - Use AVG aggretation function

SELECT 
    s.user_id,
    (ROUND(AVG(IF(c.action = 'confirmed', 1, 0)), 2)) AS confirmation_rate
FROM
    Signups AS s
LEFT JOIN
    Confirmations AS c
    ON 
        s.user_id = c.user_id
GROUP BY
    s.user_id

Tags:

Updated: