题解 | #某宝店铺动销率与售罄率#
某宝店铺动销率与售罄率
https://www.nowcoder.com/practice/715dd44c994f45cb871afa98f1b77538
with
skuList as (
select
style_id,
round(sum(sales_num)*100/(avg(allinv) - sum(sales_num)),2) `pin_rate(%)`,
round(sum(sales_price)*100/avg(allcost),2) `sell-through_rate(%)`
from
sales_tb
join
(
select
style_id,
sum(inventory) allinv, #根据styleid求出每个spu的总的库存
sum(tag_price*inventory) allcost # 根据 style_id求出每个spu的总成本
from product_tb
group by
style_id
) t1
where substr(sales_tb.item_id,1,1) = t1.style_id #将总成本和总的库存绑定到每个sku中,这样做聚类可以用avg聚合函数求出每个spu的总库存和总成本了。
group by
style_id
)
select * from skuList
本体的难度在于不能直接连接2个表做汇总,因为求总库存只能依据product_tb 中的唯一数据来做汇总。
除了我上面的代码,也可以先把sales_tb 表做每个item_id聚合汇总后求出每个item_id对应的总售卖量和总售卖价格,与product_tb连接后就可以根据style_id分类求出相关数据。

查看6道真题和解析