题解 | #10月的新户客单价和获客成本#

10月的新户客单价和获客成本

https://www.nowcoder.com/practice/d15ee0798e884f829ae8bd27e10f0d64

# 商城里所有新用户的首单平均交易金额(客单价)和平均获客成本(平均优惠价)。
# 新用户的定义?之前(10月份前)没有出现过的uid
# 字段:客单价 / 平均获客成本

# 表一:聚合订单明细表
with table1 as (
    select order_id,sum(price*cnt) as  pre_cost
    from tb_order_detail
    group by order_id
),

# 表二:连接 订单总表 & 新用户筛选
table2 AS(
    select event_time,order_id,uid,total_amount,
    row_number()OVER(partition by uid order by event_time asc) as rk,
    # COALESCE(pre_cost, 0) - total_amount AS cost
    (pre_cost - total_amount) as cost
    from tb_order_overall left join table1 using(order_id)
    where uid not in(select uid from tb_order_overall where date(event_time)<'2021-10-01') AND status = 1 

)


# 计算客单价 & 平均获客成本
select ROUND(sum(total_amount)/count(distinct order_id),1) AS avg_amount,
ROUND(sum(cost)/COUNT(DISTINCT order_id),1) AS avg_cost
FROM table2
WHERE YEAR(event_time) = 2021 AND MONTH(event_time) = 10 AND rk = 1


# 注意点:计算客单价的时候不能用avg,而是sum/count
# 注意点:首单! 需要先对table2中的数据进行uid分组,时间排序。。。rk = 1

全部评论

相关推荐

不愿透露姓名的神秘牛友
昨天 14:08
点赞 评论 收藏
分享
程序员牛肉:主要是因为小厂的资金本来就很吃紧,所以更喜欢有实习经历的同学。来了就能上手。 而大厂因为钱多,实习生一天三四百的就不算事。所以愿意培养你,在面试的时候也就不在乎你有没有实习(除非是同级别大厂的实习。) 按照你的简历来看,同质化太严重了。项目也很烂大街。 要么换项目,要么考研。 你现在选择工作的话,前景不是很好了。
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-03 18:22
投了几百份简历,专业和方向完全对口,都已读不回。尝试改了一下学校,果然有奇效。
steelhead:这不是很正常嘛,BOSS好的是即便是你学院本可能都会和聊几句,牛客上学院本机会很少了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务