最简单的代码方式
10月的新户客单价和获客成本
https://www.nowcoder.com/practice/d15ee0798e884f829ae8bd27e10f0d64
select round(avg(total_amount),1) as avg_amount, round(avg(sum_cost-total_amount),1) as avg_cost from tb_order_overall join(select order_id,sum(price*cnt) as sum_cost from tb_order_detail group by order_id ) as t using(order_id) where event_time like '2021-10%'#2021年10月 and (uid,event_time) in (select uid, min(event_time) as new from tb_order_overall group by uid) #新用户的首单
直接在join中把订单总金额汇总。按照订单来做表关联。
求平均,直接avg就可以,不用sum之后在除。