题解 | #10月的新户客单价和获客成本#
10月的新户客单价和获客成本
http://www.nowcoder.com/practice/d15ee0798e884f829ae8bd27e10f0d64
- 心得: 嘿嘿,先自夸一波,经过几天较难题目的探索,终于自己完成了一个较难的题目,表扬!再接再厉!但是代码还是不够简洁,要学会简洁代码,前两天看了个with as的用法,但目前还不够熟练,再学习一下,后面争取用简洁的代码再做一遍~
- 方法如下
- 昨天才做了一道关于新用户的题目,其实套路都是一样的,首先,将表格连接到一起,然后提取出我们需要的那几列,接着要确定用户成为新用户的日期,单独形成一列(利用窗口函数),这样就形成我们的表1了。
- 然后在表1的基础上,判断用户是否为新用户,并且要筛选出时间在2021年10月的用户。形成表2
- 在表2的基础上,计算出用户支付的总价格和商品本身的价格,形成表3
- 最后在表3的基础上,计算评价价格即可。
select
round(avg(total),1) as avg_amount,
round(avg(shiji_price-total),1) as avg_cost
from
(select
total_amount as total,
uid,
sum(price) as shiji_price
from
(select
*,
** if(dt=min_new,1,0) as if_new**
from
(select
uid,tod.order_id,tod.product_id,price,total_amount,date(event_time) as dt,**min(date(event_time)) over (partition by uid order by date(event_time)) as min_new**
from tb_order_detail tod
left join tb_order_overall too
on tod.order_id=too.order_id
left join tb_product_info tpi
on tod.product_id=tpi.product_id
)t1
where date_format(dt,'%Y-%m')='2021-10'
)t2
where if_new=1
group by uid,total_amount)t3