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

描述

商品信息表tb_product_info

idproduct_idshop_idtagint_quantityrelease_time
18001901日用6010002020-01-01 10:00:00
28002901零食1405002020-01-01 10:00:00
38003901零食1605002020-01-01 10:00:00
48004902零食1305002020-01-01 10:00:00

(product_id-商品ID, shop_id-店铺ID, tag-商品类别标签, in_price-进货价格, quantity-进货数量, release_time-上架时间)

订单总表tb_order_overall

idorder_iduidevent_timetotal_amounttotal_cntstatus
13010021022021-10-01 11:00:0023521
23010031012021-10-02 10:00:0030021
33010051042021-10-03 10:00:0016011

(order_id-订单号, uid-用户ID, event_time-下单时间, total_amount-订单总金额, total_cnt-订单商品总件数, status-订单状态)

订单明细表tb_order_detail

idorder_idproduct_idpricecnt
13010028001851
230100280031801
330100380041401
430100380031801
530100580031801

(order_id-订单号, product_id-商品ID, price-商品单价, cnt-下单数量)

问题:请计算2021年10月商城里所有新用户的首单平均交易金额(客单价)和平均获客成本(保留一位小数)。

:订单的优惠金额 = 订单明细里的{该订单各商品单价×数量之和} - 订单总表里的{订单总金额} 。

输出示例

示例数据的输出结果如下

avg_amountavg_cost
231.723.3

解释:

2021年10月有3个新用户,102的首单为301002,订单金额为235,商品总金额为85+180=265,优惠金额为30;

101的首单为301003,订单金额为300,商品总金额为140+180=320,优惠金额为20;

104的首单为301005,订单金额为160,商品总金额为180,优惠金额为20;

平均首单客单价为(235+300+160)/3=231.7,平均获客成本为(30+20+20)/3=23.3

题解:

步骤一:找到2021年10月新用户的首单交易(首单:交易时间最小min(date(event_time)),按照用户id,订单id,商品id分组查询(一个订单量可能会有好几个商品)

select uid,overall.order_id,detail.product_id,min(date(event_time)) dt,price,cnt,total_amount
from tb_order_detail detail
join tb_product_info product 
on product.product_id=detail.product_id
join tb_order_overall overall 
on detail.order_id=overall.order_id
where date_format(event_time,'%Y-%m')='2021-10'
group by uid,overall.order_id,detail.product_id,price,cnt,total_amount

 步骤二:查询出每个用户首单的平均订单金额和商品总金额(total_smount已经是总和了,所以直接查询出来)

		select uid,avg(total_amount) total_amount,sum(price*cnt) sum_amount -- 查询出每个用户首单的平均订单金额和商品总金额(total_smount已经是总和了,所以直接查询出来)
		from (
				-- 2021年10月新用户的首单交易(首单:交易时间最小),按照用户id,订单id,商品id查询
				select uid,overall.order_id,detail.product_id,min(date(event_time)) dt,price,cnt,total_amount
				from tb_order_detail detail
				join tb_product_info product 
				on product.product_id=detail.product_id
				join tb_order_overall overall 
				on detail.order_id=overall.order_id
				where date_format(event_time,'%Y-%m')='2021-10'
				group by uid,overall.order_id,detail.product_id,price,cnt,total_amount
		)t
		group by uid

步骤三: 计算2021年10月商城里所有新用户的首单平均交易金额(客单价)和平均获客成本(获客成本=price*cnt-实际总金额)

select round(AVG(total_amount),1) avg_amount,round(avg(sum_amount-total_amount),1)  avg_cost
from(
		select uid,avg(total_amount) total_amount,sum(price*cnt) sum_amount -- 查询出每个用户首单的平均订单金额和商品总金额(total_smount已经是总和了,所以直接查询出来)
		from (
				-- 2021年10月新用户的首单交易(首单:交易时间最小),按照用户id,订单id,商品id查询
				select uid,overall.order_id,detail.product_id,min(date(event_time)) dt,price,cnt,total_amount
				from tb_order_detail detail
				join tb_product_info product 
				on product.product_id=detail.product_id
				join tb_order_overall overall 
				on detail.order_id=overall.order_id
				where date_format(event_time,'%Y-%m')='2021-10'
				group by uid,overall.order_id,detail.product_id,price,cnt,total_amount
		)t
		group by uid
 )t



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值