with firsts as (
select user_id,
MIN(case when event_type='view' then event_date end) as t_view,
MIN(case when event_type='cart' then event_date end) as t_cart,
MIN(case when event_type='purchase' then event_date end) as t_purchase
from sys.events
group by user_id
),
counts as (
select count(*) users_total,
SUM(case when t_view is not null then 1 else 0 end) as step_view,
SUM(case when t_view is not null and t_cart is not null and t_cart >= t_view then 1 else 0 end) as step_cart,
SUM(case when t_cart is not null and t_purchase is not null and t_purchase >= t_cart then 1 else 0 end) as step_purchase
from firsts
)
select users_total,
step_view, step_cart, step_purchase,
ROUND(step_cart / nullif(step_view,0),4) as conv_view_to_cart,
ROUND(step_purchase / nullif(step_cart,0),4) as conv_cart_to_purchase,
ROUND(step_purchase / nullif(step_view,0),4) as conv_view_to_purchase
from counts