I have the below table:
s.userid c.userid c.action
3 3 timeout
3 3 timeout
7 7 confirmed
7 7 confirmed
7 7 confirmed
2 2 timeout
2 2 confirmed
For a given s.user_id, I am trying to calculate
count(confirmed)/ (count(confirmed) + count(timeout))
So the end result should look like:
s.userid rate
3 0
7 1
2 0.5
I saw a solution where the following query was used:
select s.user_id, avg(if(c.action = ‘confirmed’, 1, 0)) as rate from tablename group by s.user_id;
I understand that the avg() function takes an expression and the if() function returns values based on a condition. I don’t understand how this query works internally. If the if() function returns a value and the avg() function is expecting a numeric column, then what is the logical sequence of operations in this query?
2
Answers
Using conditional aggregation we can try:
If a given user might have neither confirmed nor timeout actions, then the above would give a divide by zero. In that case, assuming you would want to report the ratio as zero, we can use case logic:
This is a way to do it using
LEFT JOIN
: