CREATE TABLE ab_test (
user_id INT,
variant CHAR(1),
converted TINYINT
);
INSERT INTO ab_test (user_id, variant, converted) VALUES
(1, 'A', 1),
(2, 'A', 0),
(3, 'A', 1),
(4, 'A', 0),
(5, 'B', 1),
(6, 'B', 1),
(7, 'B', 1),
(8, 'B', 0);
--1st approach
with ct as (
select *, case when variant='A'
then count(case when converted=1 then user_id end) over(partition by variant)*1.000/count(user_id) over(partition by variant)
when variant='B'
then count(case when converted=1 then user_id end) over(partition by variant)*1.000/count(user_id) over(partition by variant)
end as cr_var
,count(case when converted=1 then user_id end) over()*1.000/count(user_id) over() as overall_cr
from ab_test
)
select user_id, variant,converted,cr_var,overall_cr
from ct
where cr_var>overall_cr
group by user_id, variant,converted,cr_var,overall_cr;
--2nd approach
with overall as (
select count(case when converted=1 then user_id end)*1.000/count(user_id) as overall_cr
from ab_test
)
,var_cr as (
select variant, count(case when converted=1 then user_id end)*1.000/count(user_id) as var_cr
from ab_test
group by variant
)
select user_id, ab.variant,converted,overall_cr,var_cr
from ab_test ab
cross join overall o
join var_cr vc on ab.variant = vc.variant
where var_cr>overall_cr
group by user_id, ab.variant,converted,overall_cr,var_cr
To embed this project on your website, copy the following code and paste it into your website's HTML: