首页 > 试题广场 >

某宝店铺动销率与售罄率

[编程题]某宝店铺动销率与售罄率
  • 热度指数:37676 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
11月结束后,小牛同学需要对其在某宝的网店就11月份用户交易情况和产品情况进行分析以更好的经营小店。
已知产品情况表product_tb如下(其中,item_id指某款号的具体货号,style_id指款号,tag_price表示标签价格,inventory指库存量):

11月份销售数据表sales_tb如下(其中,sales_date表示销售日期,user_id指用户编号,item_id指货号,sales_num表示销售数量,sales_price表示结算金额):
请你统计每款的动销率(pin_rate,有销售的SKU数量/在售SKU数量)(请忽略动销率实际计算公式,以该题目提供的公式为准)与售罄率(sell-through_rate,GMV/备货值,备货值=吊牌价*库存数),按style_id升序排序,以上例子的输出结果如下:

示例1

输入

drop table if exists product_tb;
CREATE TABLE product_tb(
item_id char(10) NOT NULL,
style_id char(10) NOT NULL,
tag_price int(10) NOT NULL,
inventory int(10) NOT NULL
);
INSERT INTO product_tb VALUES('A001', 'A', 100,  20);
INSERT INTO product_tb VALUES('A002', 'A', 120, 30);
INSERT INTO product_tb VALUES('A003', 'A', 200,  15);
INSERT INTO product_tb VALUES('B001', 'B', 130, 18);
INSERT INTO product_tb VALUES('B002', 'B', 150,  22);
INSERT INTO product_tb VALUES('B003', 'B', 125, 10);
INSERT INTO product_tb VALUES('B004', 'B', 155,  12);
INSERT INTO product_tb VALUES('C001', 'C', 260, 25);
INSERT INTO product_tb VALUES('C002', 'C', 280,  18);

drop table if exists sales_tb;
CREATE TABLE sales_tb(
sales_date date NOT NULL,
user_id int(10) NOT NULL,
item_id char(10) NOT NULL,
sales_num int(10) NOT NULL,
sales_price int(10) NOT NULL
);

INSERT INTO sales_tb VALUES('2021-11-1', 1, 'A001',  1, 90);
INSERT INTO sales_tb VALUES('2021-11-1', 2, 'A002',  2, 220);
INSERT INTO sales_tb VALUES('2021-11-1', 2, 'B001',  1, 120);
INSERT INTO sales_tb VALUES('2021-11-2', 3, 'C001',  2, 500);
INSERT INTO sales_tb VALUES('2021-11-2', 4, 'B001',  1, 120);
INSERT INTO sales_tb VALUES('2021-11-3', 5, 'C001',  1, 240);
INSERT INTO sales_tb VALUES('2021-11-3', 6, 'C002',  1, 270);
INSERT INTO sales_tb VALUES('2021-11-4', 7, 'A003',  1, 180);
INSERT INTO sales_tb VALUES('2021-11-4', 8, 'B002',  1, 140);
INSERT INTO sales_tb VALUES('2021-11-4', 9, 'B001',  1, 125);
INSERT INTO sales_tb VALUES('2021-11-5', 10, 'B003',  1, 120);
INSERT INTO sales_tb VALUES('2021-11-5', 10, 'B004',  1, 150);
INSERT INTO sales_tb VALUES('2021-11-5', 10, 'A003',  1, 180);
INSERT INTO sales_tb VALUES('2021-11-6', 11, 'B003',  1, 120);
INSERT INTO sales_tb VALUES('2021-11-6', 10, 'B004',  1, 150);

输出

A|8.33|7.79
B|14.81|11.94
C|10.26|8.75
我一个电商运营出身的,都没搞明白他想要的动销率是啥,动销率不就是考虑店内有销售的SKU数量占比吗,按他算出来的这个数,他这个店开了干啥,关门得了
发表于 2022-05-19 19:02:28 回复(5)
实际上,我觉得解决这个问题的一个难点就是要理解他要求什么样的指标----动销率和售罄率以及这两个指标是怎么去构建的,尤其是怎样去构建这一方面非常重要。
题目中的动销率就是每个style_id的已经销售的数量/(每个style_id的库存量-已经销售的数量),
售罄率就是每个style_id的GMV(就是sum(sales_price))/(吊牌价*库存数量),库存数量就是一开始的库存数量,我之前理解的是需要减去已经销售出去的数量,实际上是不需要的减去的,就是原始的库存数量。
整个程序如下:

select a.style_id,
round(b.ys_cnt/(a.kc_cnt-b.ys_cnt)*100,2) 'pin_rate(%)',
round(b.GMV/(a.bh_cnt)*100,2) 'sell-through_rate(%)'
from
(select style_id,sum(inventory) kc_cnt,sum(inventory*tag_price) bh_cnt from product_tb group by style_id) as a
left join
(select p.style_id,sum(s.sales_price) GMV,sum(s.sales_num) ys_cnt from product_tb p 
left join sales_tb s on p.item_id=s.item_id group by p.style_id) as b on b.style_id=a.style_id
order by a.style_id asc


发表于 2021-12-12 11:20:04 回复(4)
# 难点就是理解 动销率和售罄率 
select style_id, round(sum(sku) / sum(inventory-sku)*100,2) pin_rate,
                 round(sum(gmv) / sum(inventory*tag_price)*100,2) sell_through_rate
FROM
    (SELECT item_id,sum(sales_num) sku ,sum(sales_price) gmv
     FROM sales_tb
     WHERE sales_date BETWEEN '2021-11-01' and '2021-11-30'
     GROUP BY item_id) t 
JOIN product_tb pt USING(item_id)
GROUP by style_id
ORDER by style_id
发表于 2021-12-22 15:50:25 回复(0)
select style_id,
round(sum(sales_num)/(sum(distinct inventory)-sum(sales_num))*100,2),
round(sum(sales_price)/sum(distinct((tag_price*inventory)))*100,2)
from product_tb pt
inner join sales_tb st
on pt.item_id=st.item_id
group by style_id
发表于 2021-12-18 21:34:24 回复(10)

【场景】:分组求和、动销率 = 销售的货物数量/(库存数量减去已经销售的货物数量)、售罄率 = 结算金额/(吊牌价格*库存数)

【分类】:分组查询

分析思路

难点:

1.理解动销率和售罄率

(1)统计每个货号的销售数量和总的销售金额

(2)计算动销率和售罄率

求解代码

方法一

with子句

with
    main as(
        #统计每个货号的销售数量和总的销售金额
        select
            item_id,
            sum(sales_num) sku ,
            sum(sales_price) gmv
        from sales_tb
        group by item_id
    )

#动销率 = 销售的货物数量/(库存数量减去已经销售的货物数量)
#售罄率 = 结算金额/(吊牌价格*库存数)
select
    style_id, 
    round(sum(sku) / sum(inventory-sku)*100,2) pin_rate,
    round(sum(gmv) / sum(inventory*tag_price)*100,2) sell_through_rate
from main
join product_tb pt using(item_id)
group by style_id
order by style_id

方法二

from子查询

select
    style_id, 
    round(sum(sku) / sum(inventory-sku)*100,2) pin_rate,
    round(sum(gmv) / sum(inventory*tag_price)*100,2) sell_through_rate
from(
    #统计每个货号的销售数量和总的销售金额
    select
        item_id,
        sum(sales_num) sku ,
        sum(sales_price) gmv
    from sales_tb
    group by item_id) main
join product_tb pt using(item_id)
group by style_id
order by style_id
发表于 2022-11-25 18:03:36 回复(0)
动销率直接说 销售量/库存 就完事了呗,干嘛这么拐弯抹角的
发表于 2023-08-10 16:53:27 回复(0)
只用一次多表连接
select style_id
        ,round(sum(sales_num)/(sum(distinct inventory)-sum(sales_num))*100,2)as "pin_rate(%)"
        ,round(sum(b.sales_price)/sum(distinct a.tag_price*a.inventory)*100,2) as "sell-through_rate(%)"
from product_tb a  join sales_tb b 
on a.item_id=b.item_id
group by style_id
order by style_id

发表于 2022-07-27 21:04:33 回复(7)
对不起,写答案的人根本不知道什么叫动销率。按照答案的这种算法,卖到一半发现他所谓的‘动销率’超过100%了,这不开玩笑呢吗。
发表于 2023-03-24 19:35:09 回复(1)
with a as
(
    select
    style_id
    ,sum(inventory) inventory_total
    ,sum(tag_price * inventory) inventory_price
    from product_tb
    group by 1
),
    b as
(
    select
    style_id
    ,sum(sales_num) sales_num_total
    ,sum(sales_price) gmv
    from sales_tb
    left join product_tb p using(item_id)
    group by 1
)
  
select
style_id
,round(sales_num_total/(inventory_total - sales_num_total)*100,2) pin_rate
,round(gmv/inventory_price*100,2) sell_through_rate
from a
join b 
using(style_id)
order by 1

#参考评论区建立新表,方便对没啥关系的库存和售出分别乘积求和

发表于 2021-12-20 21:48:56 回复(0)
select 
    distinct style_id
    ,round(s_num/(num-s_num)*100,2)
    ,round(GMV/b_GMV*100,2)
from 
    (select style_id,item_id
        ,sum(inventory)over(partition by style_id) as num
        ,sum(tag_price*inventory)over(partition by style_id) as b_GMV 
     from product_tb) a 
left join  
    (select item_id
         ,sum(sales_num)over(partition by substring(item_id,1,1)) as s_num
         ,sum(sales_price)over(partition by substring(item_id,1,1)) as GMV 
     from sales_tb where month(sales_date)=11) b 
on a.item_id=b.item_id
order by a.style_id
感觉题目的难点就是在一些名词的理解,题目中的动销率,在网上搜索是“有销量的商品数量/所有商品数量”,而此题里的动销率计算是“有销量商品销售数量/剩余仍在销售的商品数量”,一开始把“在售商品数量”理解成原来一共具有的商品数了,哪知道是剩余还在售的商品数量。
发表于 2022-04-09 19:54:40 回复(1)
select style_id
    ,round(sum(s)/sum(inventory-s)*100,2) 
    ,round(sum(g)/sum(inventory*tag_price)*100,2) 
from(
    select
        item_id 
        ,sum(sales_num) s 
        ,sum(sales_price) g
    from sales_tb
    group by 1) a join product_tb using(item_id)
group by 1
order by 1;

发表于 2024-08-04 15:07:13 回复(0)
#此动销率非彼动销率
#动销率=销售的商品个数/库存剩余的商品个数
with t0 as
 (
    select style_id,
    sum(tag_price*inventory) as inventory_price,
    sum(inventory) as inventory_total
from product_tb
group by style_id
),
t1 as 
(
    select 
    style_id,
    sum(sales_num) as sales_num_total,
    sum(sales_price) as gmv
from product_tb a left join sales_tb b on a.item_id=b.item_id
group by style_id
)

select 
style_id,
round(sales_num_total/(inventory_total-sales_num_total)*100,2),
round(gmv/inventory_price*100,2)
from t0 
join t1 using(style_id)
order by style_id

发表于 2023-10-23 22:08:58 回复(0)
select
    style_id,
    round(sum(sales_num) / (sum(distinct inventory) - sum(sales_num)) * 100,2) 'pin_rate(%)',
    round(sum(sales_price) / sum(distinct tag_price * inventory) * 100,2) 'sell-through_rate(%)'
from product_tb p
left join sales_tb s
on p.item_id = s.item_id
group by style_id
order by style_id;

发表于 2023-08-05 11:20:02 回复(2)
-- 建立临时表,两次分类计算再连接
先计算各类商品的销量和售出总额 记作temp1
with temp1 as 
(
select 
style_id,
sum(sales_num) as sell,
sum(sales_price) as GMV
from sales_tb a
left join product_tb b using(item_id)
group by style_id
)
建立表二
计算各类商品的库存和备货值(库存*标签价格) 记作temp2
temp2 as 
(
select 
style_id,
sum(inventory) as total_inventory,
sum(tag_price*inventory) as beihuozhi
from product_tb a
group by style_id
)
连接这两张表
动销率=售出件数/(库存-售出)-- 注意这里要减去售出
售罄率=售出金额/备货值  -- 注意这里计算备货值时的库存不用减售出
select 
style_id,
round(100*sell/(total_inventory-sell),2) as 'pin_rate(%)',
round(100*GMV/beihuozhi,2) as 'sell-through_rate(%)'
from temp2
left join temp1 using(style_id)
order by style_id
完整代码如下
with temp1 as 
(
select 
style_id,
sum(sales_num) as sell,
sum(sales_price) as GMV
from sales_tb a
left join product_tb b using(item_id)
group by style_id
)
,temp2 as 
(
select 
style_id,
sum(inventory) as total_inventory,
sum(tag_price*inventory) as beihuozhi
from product_tb a
group by style_id
)

select 
style_id,
round(100*sell/(total_inventory-sell),2) as 'pin_rate(%)',
round(100*GMV/beihuozhi,2) as 'sell-through_rate(%)'
from temp2
left join temp1 using(style_id)
order by style_id
发表于 2022-05-17 23:45:07 回复(3)
select
style_id
,round(sum(sales_num) / ( sum(distinct inventory) - sum(sales_num) )*100,2)
,round(sum(sales_price)*100 / sum(distinct(tag_price*inventory)),2)
from sales_tb
left join product_tb
using (item_id)
where sales_date like '2021-11%'
group by 1
发表于 2025-05-20 09:43:12 回复(0)
# with temp1 as
# (
#     select style_id,sum(sales_num) as sales_out,sum(distinct inventory)-sum(sales_num) as sales_on,
# sum(sales_price) as GMV,sum(distinct tag_price*inventory) as inventory_value
# from
# product_tb left join sales_tb using(item_id)
# group by style_id
# )
# select style_id,round(100*sales_out/sales_on,2) as pin_rate,
# round(100*GMV/inventory_value,2) as sell_through_rate
# from temp1

# 按照上面的代码,连接后的表,会有多条tag_price、inventory记录,因此在计算sum(inventory)和sum(tag_price*inventory)数据都会偏大,可以在tag_price、inventory前面用distinct去重,但是这种方法不通用,对原始数据的要求较高,一旦product_tb表的A组内有两个item_id的tag_price值一样就不可行
select t1.style_id,round(100*sales_out /(total_inventory-sales_out),2) as pin_rate,
round(100*GMV/inventory_value,2) as sell_through_rate
from
(
    select style_id,sum(tag_price*inventory) as inventory_value,sum(inventory) as total_inventory
from product_tb group by style_id
)as t1
left join
(
    select style_id,sum(sales_num) as sales_out,sum(sales_price) as GMV
from product_tb left join sales_tb using (item_id) group by style_id
) as t2
on t1.style_id=t2.style_id
发表于 2025-05-09 15:22:26 回复(0)
#求每个item_id的总销售数量和总销售额
with t1 as
(
select
    item_id,
    sum(sales_num) as sku,
    sum(sales_price) as gmv
from sales_tb
group by item_id
),
#和product_tb进行连接
t2 as
(
select
    *
from t1
join product_tb
using(item_id)
)
#按题意,pin_rate=每个style_id的销售数量/(每个style_id的库存-每个style_id的销售数量),sell-through_rate=每个style_id的销售额/每个style_id的备货值
select
    style_id,
    round(sum(sku)/sum(inventory-sku)*100,2) as pin_rate,
    round(sum(gmv)/sum(inventory*tag_price)*100,2) as sell_through_rate
from t2
group by style_id
order by style_id

发表于 2025-04-14 12:16:34 回复(0)
select style_id,
round(sum(sales_num)/(avg(style_inventory_cnt)-sum(sales_num))*100,2) 'pin_rate(%)',
round(sum(sales_price)/avg(through)*100,2) 'sell_through_rate(%)'
from sales_tb
inner join (
    select *,
    sum(inventory)over(partition by style_id) style_inventory_cnt,
    sum(tag_price*inventory)over(partition by style_id) through
    from product_tb
)t1
on sales_tb.item_id=t1.item_id
group by style_id
order by 1;

发表于 2025-03-24 09:20:45 回复(0)
SELECT 
    style_id,
    ROUND(SUM(total_sales_num) / (SUM(inventory - total_sales_num)) * 100, 2) AS pin_rate,
    ROUND(SUM(total_sales_price) / (SUM(inventory * tag_price)) * 100, 2) AS sell_through_rate
FROM (
    SELECT 
        item_id,
        SUM(sales_price) AS total_sales_price,
        SUM(sales_num) AS total_sales_num
    FROM sales_tb 
    GROUP BY item_id
) AS A
LEFT JOIN product_tb USING(item_id)
GROUP BY style_id
ORDER BY style_id

发表于 2025-03-03 10:52:00 回复(0)
select style_id,
round((sum(sales_num)/(max(skc)-sum(sales_num)))*100,2),
round((sum(sales_price)/max(bhz))*100,2)
from product_tb pt join sales_tb st using(item_id)
join
(select style_id,sum(inventory) skc,sum(tag_price*inventory) bhz from product_tb group by style_id) t1 using(style_id)
group by style_id
发表于 2024-12-26 00:53:12 回复(0)

问题信息

难度:
131条回答 1448浏览

热门推荐

通过挑战的用户

查看代码
某宝店铺动销率与售罄率