I have one list of events. One event name is creating an account and another is creating an account with Facebook. I am trying to see what percentage of accounts created use Facebook.
The code below will give me an accurate count of the number of facebook accounts and total accounts, but when I try to divide the two numbers it just gives me the number 1.
I am very new to SQL, and have spent hours trying to figure out why it is doing that to no avail.
with fb_act as ( select * from raw_event where name = 'onboard_fb_success' and event_ts::date >= current_date - 30 ), total_act as ( select * from raw_event where name ='create_account' and event_ts::date >= current_date - 30 ) select count(fb_act)/count(total_act), total_act.event_ts::date as day from total_act, fb_act group by day order by day
I expect the output to be about ~.3, but the actual output is always exactly 1.
Advertisement
Answer
Conditional aggregation is a much simpler way to write the query. You appear to be using Postgres, so something like this:
select re.event_ts::date as day, (sum( (name = 'onboard_fb_success' and event_ts::date >= current_date - 30):: int) / sum( name = 'create_account' and event_ts::date >= current_date - 30)::int) ) as ratio from raw_event re group by re.event_ts::date order by day;