变量理解

  • 销售的SKU——销售量sales_num
  • 在售的SKU——库存量-销售量inventory-sales_num
  • GMV——销售额sales_price
  • 备货值——库存货价值tag_price*inventory

    易错点

  • 若先连接两表,再直接group by style_id,会出现多条同一货物的库存信息,大致如图:

图片说明

故应

    1. group by item_id,计算各货物的总销售量及总销售金额
    2. 连接两表
    3. group by style_id
  • 另,尽量使用left join,以防存在货物尚未售出,只存在product_tb中而在sales_tb中无销售记录的情况
    select style_id,
    round(100*sum(item_num)/(sum(inventory)-sum(item_num)),2) as 'pin_rate(%)',
    round(100*sum(item_price)/sum(tag_price*inventory),2) as 'sell-through_rate(%)'
    from product_tb as q1
    left join
    (select item_id,sum(sales_num) as item_num,
    sum(sales_price) as item_price
    from sales_tb
    group by item_id) as q2
    on q1.item_id=q2.item_id
    group by style_id
    order by style_id
    

```