首页 > 试题广场 >

某宝店铺动销率与售罄率

[编程题]某宝店铺动销率与售罄率
  • 热度指数:40819 时间限制: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)
动销率直接说 销售量/库存 就完事了呗,干嘛这么拐弯抹角的
发表于 2023-08-10 16:53:27 回复(0)

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

【分类】:分组查询

分析思路

难点:

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)
只用一次多表连接
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)
with t1 as (
select style_id,sum(inventory) as inv_cnt,sum(tag_price*inventory) as bhz
from product_tb
group by 1
),t2 as (
select style_id,sum(sales_num) as sales_cnt,sum(sales_price) as GMV
from product_tb
join sales_tb
using(item_id)
group by 1
)
select 
t1.style_id
,round((sales_cnt)/(inv_cnt-sales_cnt)*100,2) as "pin_rate(%)"
,round(GMV/bhz*100,2) as "sell-throught_rate(%)"
from t1 
join t2
using(style_id)
order by 1

发表于 2025-10-14 12:56:48 回复(0)
1、销售数据表连接产品表,最后一style_id分组
with t1 as(
    select *
    from 
    product_tb left join
    (
    select 
    style_id,sum(inventory) as in_sum,sum(tag_price*inventory) as tag_price_sum
    from product_tb
    group by style_id
    ) a
    using(style_id)
),
t2 as (
select 
style_id, tag_price, inventory, in_sum, sales_num, sales_price, tag_price_sum
from sales_tb
left join t1
using(item_id)
)
select 
style_id,
round(sum(sales_num)/(avg(in_sum)-sum(sales_num))*100,2) AS pin_rate,
round(sum(sales_price)/avg(tag_price_sum)*100,2) as sell_through_rate
from t2
group by style_id
order by style_id
2、先以item_id分组计算销售数据表,然后连接产品表,以style_id分组
select
style_id,
round(100*sum(sales_online)/(sum(inventory)-sum(sales_online)),2),
round(100*sum(sales_price_sum)/sum(tag_price*inventory),2)
from
product_tb
left join
(
select 
item_id,
sum(sales_num) as sales_online,
sum(sales_price) as sales_price_sum
from sales_tb
group by item_id
) b
using(item_id)
group by style_id
order by style_id



发表于 2025-10-12 23:48:39 回复(0)
-- 第一种
select
    style_id
    , round(sum(sales_num) / (sum(distinct inventory)-sum(sales_num)) * 100, 2) as 'pin_rate(%)'
    , round(sum(sales_price) / sum(distinct p.tag_price * p.inventory) * 100, 2) as '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
;
-- 第二种
with
style_info as
(
    select
        style_id
        , sum(inventory) as tot_inventory
        , sum(tag_price * inventory) as pre_amount
    from product_tb
    group by style_id
),
style_sales as
(
    select
        style_id
        , sum(sales_num) as tot_sales
        , sum(sales_price) as GMV
    from sales_tb s
        left join product_tb p on s.item_id = p.item_id
    group by style_id
)
select
    t1.style_id
    , round(tot_sales / (tot_inventory - tot_sales) * 100, 2) as 'pin_rate(%)'
    , round(GMV / pre_amount * 100, 2) as 'sell-through_rate(%)'
from style_info t1
    left join style_sales t2 on t1.style_id = t2.style_id
order by t1.style_id
;


发表于 2025-10-02 14:06:39 回复(0)
select distinct style_id,
round(100*y/(x-y),2)  'pin_rate(%)',
round(100*n/m,2) 'sell-through_rate(%)'
from 
(select item_id,style_id,
sum(inventory) over(partition by style_id) as x,
sum(tag_price*inventory) over(partition by style_id) as m
from product_tb
) as a
left join 
(select item_id,
sum(sales_num) over(partition by left(item_id,1)) as y,
sum(sales_price) over(partition by left(item_id,1)) as n
from sales_tb
) as b
on a.item_id = b.item_id
order by style_id

发表于 2025-09-01 10:37:46 回复(0)
真就是为了出题而出题呗,已售数量/(库存数-已售量),但凡已售超过库存数一半,动销率就是百分之百往上了,有什么意义
发表于 2025-08-24 10:11:20 回复(0)
with a as
(
    select   item_id,sum(sales_num) total_sell,sum(sales_price) GMV
    from sales_tb
    group by item_id
),
b as(
    select pt.inventory-a.total_sell containing_goods,a.item_id,pt.style_id,tag_price*inventory beihuozhi
    from product_tb pt
    left join a
    on  a.item_id=pt.item_id
)

select style_id,round(sum(total_sell)/sum(containing_goods)*100,2),
round(sum(GMV)/sum(beihuozhi)*100,2)
from a
left join b
on a.item_id=b.item_id
group by style_id

发表于 2025-08-13 18:05:40 回复(0)