# 明确问题:
# 1.统计每款的动销率与售罄率
# 2.按style_id升序排序
# 问题拆解:
# 1.指标:
# 动销率=有销售的sku(库存数量)/在售库存数量=销售数量/总库存量-销售数量
# 售罄率=GMV/(吊牌价*库存数)
# 2.聚合字段:style_id
# 易错点:不要连接之后再求inventory_price
with a as (
  SELECT style_id
  ,SUM(tag_price * inventory) inventory_price
  ,sum(inventory) total_inventory
  FROM product_tb
  GROUP BY style_id
),
b as (
  select style_id
  ,sum(sales_price) GMV
  ,sum(sales_num) sales_num_total
  from product_tb a
  inner join sales_tb b
  on a.item_id=b.item_id
  group by style_id
)
select
a.style_id,
  ROUND(sales_num_total / (total_inventory - sales_num_total) * 100, 2) pin_rate,
  ROUND(GMV / inventory_price * 100, 2) sell_through_rate
from a
inner join b
on a.style_id=b.style_id