比较简单的题,子查询计数,然后进行统计清晰明了,要注意筛查分母为0 的情况
SELECT product_id,round(click/showtimes,3) as ctr,
if(click=0,0,round(cart/click,3)),
if(cart=0,0,round(payment/cart,3)),
if(payment=0,0,round(refund/payment,3)) as refund_rate
FROM
(select product_id,
count(*) as showtimes,
sum(if_click) as click,
sum(if_cart) as cart,sum(if_payment) as payment,
sum(if_refund) as refund
FROM tb_user_event
where date_format(EVENT_time,"%Y-%m")="2021-10"
group by product_id) BASE
group by product_id
having refund_rate<=0.5
order by product_id