首页 > 试题广场 >

某宝店铺动销率与售罄率

[编程题]某宝店铺动销率与售罄率
  • 热度指数: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
select t1.style_id,
       round(100*sum_sn/(sum_inv-sum_sn),2) as `pin_rate(%)`,
       round(100*sum_sp/sum_invp,2) as `sell-through_rate(%)`
       from (
       select style_id,sum(sales_num) sum_sn,sum(sales_price) sum_sp from sales_tb st
       left join product_tb pt using(item_id) group by style_id
       )t1 join (
       select style_id,sum(inventory) sum_inv,sum(inventory*tag_price) sum_in***bsp;from product_tb group by style_id
       )t2 using(style_id);这个题目容易出错,第一次直接用表2join表1,然后直接算聚合,总是不对,原来是表二中的item_id是会重复的,直接算的话会出错。
发表于 2024-01-07 01:05:40 回复(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)
select
    style_id,
    round(100 * sum(sales_num) / (sum_inventory - sum(sales_num)), 2) 'pin_rate(%)',
    round(100 * sum(sales_price) / stock_value, 2) 'sell-through_rate(%)'
from sales_tb st left join
    (
    select
        item_id,
        style_id,
        tag_price,
        inventory,
        sum(inventory) over(partition by style_id order by style_id) sum_inventory,
        sum(tag_price * inventory) over(partition by style_id order by style_id) stock_value
    from product_tb
) npt on st.item_id = npt.item_id

group by style_id, sum_inventory, stock_value
order by style_id

/*pin_rate  有销售的SKU数量 / 在售SKU数量

有销售的SKU数量:售出的SKU数量总和(商品期间销售数量

在售的SKU数量 :剩余(总库存 - 售出)库存SKU的数量总和(商品期末库存数量)

/*sell-through_rate, GMV/备货值,备货值 = 吊牌价(tag_price) * 库存数(inventory)

GMV :所有订单的金额总和(很多场景下,下单未付款的订单金额也计算在内)

吊牌价 :商品详情页显示的价格*/
/*本题利用窗口函数先计算出备货值和出售前商品数量,与销售表左连接成一个新表进行组合计算*/

发表于 2023-06-12 12:15:06 回复(0)
自己的坑,sales_price是包括sales_num在内的gmv,不需要再×num

with pt as
(
    -- 防止笛卡尔积,聚合到style_id粒度
    select
        style_id,
        sum(inventory) as inventory,
        sum(tag_price*inventory) as gmv_ori
    from product_tb
    group by 
        style_id
),
st as
(
    -- 防止笛卡尔积,聚合到style_id粒度
    select
        style_id,
        sum(sales_num) as sales_num,
        sum(sales_price) as gmv
    from sales_tb tmp
    left join product_tb tmp1 
    on tmp.item_id = tmp1.item_id
    group by style_id
) 

select
    pt.style_id,
    round(sum(st.sales_num)/sum((pt.inventory-st.sales_num))*100,2) as pin_rate,
    round(sum(st.gmv)/sum(pt.gmv_ori)*100,2) as sell_through_rate
from pt
left join st
on pt.style_id = st.style_id
group by pt.style_id
order by pt.style_id asc



发表于 2023-03-29 13:37:39 回复(0)
with
    t1 as (
        select
            style_id,
            count(distinct item_id) as total_sku,
            sum(tag_price * inventory) as style_inv_price
        from
            product_tb
        group by
            style_id
    ),
    t2 as (
        select
            p.style_id,
            count(distinct s.item_id) as sale_sku,
            sum(sales_price) as style_gmv
        from
            product_tb p
            inner join sales_tb s on p.item_id = s.item_id
        group by
            p.style_id
    )
select
    t1.style_id,
    round(t2.sale_sku * 100.0 / t1.total_sku, 2) as pin_rate,
    round(t2.style_gmv / t1.style_inv_price * 100, 2) as sell_through_rate
from
    t1,
    t2
where
    t1.style_id = t2.style_id
order by
    t1.style_id;



根据正确的动销率公式,代码应该是这样的
发表于 2023-03-10 19:07:45 回复(0)
出题人出题时假酒喝了不少啊!!!坑点:指标理解,出题人理解的实际公式为
(1)动销率 = 售出商品的数量/剩余库存的数量
(2)售罄率 = 销售商品售价(?!!!)/备货值
属实脑淤血
select
    p.style_id,
    round(sum(s.sales_num)/((select sum(inventory) from product_tb where style_id = p.style_id) - sum(s.sales_num))*100,2) "pin_rate(%)",
    round(sum(s.sales_price)/(select sum(tag_price*inventory) from product_tb where style_id = p.style_id)*100,2) "sell_through_rate(%)"
from product_tb p left join  sales_tb s using(item_id)
group by p.style_id
order by p.style_id


发表于 2023-01-03 19:12:09 回复(0)
select a.style_id,
round(100*sum(b.sales_num)/(sum(distinct a.inventory)-sum(b.sales_num)),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, sales_tb b
where a.item_id = b.item_id
group by style_id
order by style_id 

发表于 2022-12-08 04:00:06 回复(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(sku)/sum(inventory-sku)*100,2) as pin_rate,
round(sum(gmv)/sum(inventory*tag_price)*100,2) as sell_through_rate
from (
    select item_id,sum(sales_num) as sku,sum(sales_price) as gmv
    from sales_tb 
    where date_format(sales_date,'%Y-%m') = '2021-11'
    group by item_id
)t
inner join product_tb p 
on t.item_id = p.item_id
group by style_id
order by style_id;

发表于 2022-11-24 08:58:55 回复(0)
动销率这个概念有点搞,而且product是备货表,不是此刻的数据
SELECT t1.style_id, 
	ROUND(t1.total_sales * 100 / (t2.total_inventory - t1.total_sales), 2) pin_rate,
	ROUND(t1.GMV * 100/ t2.total_price, 2) sell_through_rate
FROM
(
	SELECT p.style_id style_id, 
		SUM(s.sales_num) total_sales, SUM(s.sales_price) GMV
	FROM product_tb p
	LEFT JOIN sales_tb s
	ON s.item_id = p.item_id
	GROUP BY p.style_id
) t1
JOIN
(
	SELECT style_id, SUM(inventory) total_inventory, SUM(tag_price * inventory) total_price
	FROM product_tb
	GROUP BY style_id
) t2
ON t1.style_id = t2.style_id
ORDER BY t1.style_id


发表于 2022-11-09 21:29:19 回复(0)
关键:
1.获取每个货号当前总的销售数量、总的结算金额。
select item_id,sum(sales_num) nums,sum(sales_price) prices from sales_tb group by item_id
2.理解名词含义。
动销率=有销售的SKU数量/在售SKU数量
有销售的SKU数量:每款商品的销售量
在售SKU数量:每款商品库存量-销售量

售罄率:GMV/备货值
GMV:每款商品销售总额
备货值:吊牌价(target_price)*库存量(inventory)

代码:
select style_id,round(sum(nums)*100/sum(inventory-nums),2),round(sum(prices)*100/sum(inventory*tag_price),2)
from product_tb t1
left join (select item_id,sum(sales_num) nums,sum(sales_price) prices from sales_tb group by item_id) t2
on t1.item_id=t2.item_id
group by style_id
order by style_id



发表于 2022-09-28 19:12:37 回复(0)
select t2.style_id,
round(t1.sold_sku * 100/ (t2.inv_sku - t1.sold_sku),2) as 'pin_rate(%)',
round(t1.GMV * 100/ t2.inv_value,2) as 'sell-through_rate(%)'
from (
    select p.style_id, sum(sales_num) as sold_sku,
    sum(sales_price) as GMV
    from sales_tb s join product_tb p 
    on s.item_id = p.item_id
    group by p.style_id
) t1
join (
    select style_id, sum(inventory) as inv_sku,
    sum(tag_price * inventory) as inv_value
    from product_tb
    group by style_id
) t2
on t1.style_id = t2.style_id
order by 1 asc
发表于 2022-09-09 22:56:08 回复(0)
select 
a.style_id,
ifnull(round(b.nums/(a.nums1-b.nums)*100,2),0),
ifnull(round(b.gmv/a.bh*100,2),0)
from 
(
select style_id ,
sum(inventory) nums1,
sum(tag_price*inventory) bh from product_tb group by style_id 
) a 
left join 
(
select 
substr(item_id,1,1) style_id,
sum(sales_num) nums,
sum(sales_price) gmv
from sales_tb 
where substr(sales_date,1,7) = '2021-11'
group by style_id ) b 
on a.style_id = b.style_id
order by a.style_id

发表于 2022-09-07 10:07:45 回复(0)
-- GMV指的是成交金额
select style_id,
round(s_num/(s_inv-s_num)*100,2) pin_rate,
round(s_price/s_p_inv*100,2) sell_through_rate
from 
    (select left(item_id,1) style_id,left(sales_date,7) dt,
    sum(sales_num) s_num, sum(sales_price) s_price
    from sales_tb group by left(item_id,1),dt) m
left join 
    (select style_id,sum(inventory) s_inv,sum(tag_price*inventory) s_p_inv
     from product_tb group by style_id) n
using(style_id)
where dt='2021-11'

发表于 2022-09-03 16:22:21 回复(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)
select a.style_id,
round(sales*100/(sum(inventory)-sales),2) as 'pin_rate(%)',
round(GMV*100/(sum(inventory*tag_price)),2)
from product_tb as a join 
(
select style_id,
sum(sales_num) sales,
sum(sales_price) GMV
from product_tb as a
join sales_tb as b on a.item_id=b.item_id
group by style_id) b on a.style_id=b.style_id
group by a.style_id
终于弄好了,主要是这个指标意思太难理解了
发表于 2022-07-20 23:55:22 回复(0)
WITH a AS(
  SELECT
    style_id, 
    SUM(inventory) inventory_total,
    SUM(tag_price * inventory) inventory_price,
    SUM(sales_num) sales_num_total,
    SUM(sales_price) GMV
  FROM sales_tb
  LEFT JOIN product_tb p USING(item_id)
  GROUP BY 1
)
# 这样一对多并表,sales_tb表会有重复项,所以最终结果数值就多了。
# 解决方法:分开统计,避免并表出现重复值


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
ORDER BY 1

发表于 2022-07-13 23:49:49 回复(0)
# 动销率 --- 有销售 / 在售(库存-已销售)
# 售罄率 --- GMV / 备货值
select style_id, round(sum(sales_num)/(sum(distinct inventory)-sum(sales_num))*100, 2) as `pin_rate(%)`, round(sum(sales_price)/sum(distinct tag_price*inventory)*100, 2) as `sell-through_rate(%)` from sales_tb join product_tb using(item_id) group by style_id order by style_id
;

发表于 2022-07-03 07:19:34 回复(0)
#建立临时表,按item_id聚合得到关于item_id的总销售数量和结算金额
with total_sales as (select item_id,
                    sum(sales_num) as total_num,
                    sum(sales_price) as total_price
                    from sales_tb
                    group by item_id)
#再用style_id聚合得到关于style_id的动销率和售空率
select pt.style_id,
       round(sum(total_num)/(sum(inventory)-sum(total_num))*100,2) 'pin_rate(%)',
       round(sum(total_price)/ sum(inventory*tag_price)*100,2) 'sell-through_rate(%)'
from total_sales as ts
left join product_tb as pt
on ts.item_id = pt.item_id
group by style_id
order by style_id 
这题要理解这个指标咋算的真的好难...我做了一年电商了还是看不懂这个指标咋算
发表于 2022-06-25 15:05:36 回复(0)