SQL复购率问题
表:order_detail
字段:order_id user_id product_id price cnt order_date
复购率 = 某商品近90天内购买至少2次的人数 / 90天内购买过该商品的人数
需求:近90天的最大日期以订单表里最后的日期
期望结果:
product_id 复购率
1 select 2 product_id, count(distinct case when ct >= 2 then user_id else null end) / count(distinct user_id) 3 from ( 4 select product_id,user_id,count(*) ct 5 from ( 6 select product_id,user_id,order_date,max(order_date) over() max_date from order_detail 7 ) a where datediff(max_date,order_date) <= 89 8 group by product_id,user_id 9 ) b group by product_id
浙公网安备 33010602011771号