第一步 得到所有满足条件的订单(用户初次,2021年10月)的金额和优惠金额表t1
第二步 对得到的金额和优惠金额求平均
with t1 as
(select min(total_amount) amount,sum(price*cnt)-min(total_amount) cost
from tb_order_detail t1
join tb_order_overall t2
using (order_id)
where date_format(event_time,'%Y%m')='202110'
and status =1
and (uid,event_time) in (select uid,min(event_time) from tb_order_overall
group by uid )
group by order_id)
select round(avg(amount),1) avg_amount,
round(avg(cost),1) avg_cost
from t1