sql--测试商品的重要度,是否需要及时补货

表1:商品表

表2:商品售卖表

需求:算出商品的平均点击率、平均销售、商品受欢迎度

1.使用inner join查出每件商品的点击率和销售额度

select a.ptype, a.pname, a.pview, ifnull(b.sales, 0) as selas
      from test a
               left join test_sell b on a.pid = b.id
      order by a.ptype desc, a.pview DESC

结果:

2.查出每个商品类的平均点击率

 select ptype, sum(pview) / count(*) as avg_pview from test group by ptype

 

3.查出每个商品类的平均销售额度

     select ptype,round(sum(selas) / count(*), 0) as avg_sales
      from (select a.ptype, a.pname, a.pview, ifnull(b.sales, 0) as selas
            from test a
                     left join test_sell b on a.pid = b.id
            order by a.ptype desc, a.pview DESC) a
      where a.selas > 0
      group by ptype

 

总sql:

select a.ptype,pname,pview,avg_pview,(pview/avg_pview)*0.3,selas,avg_sales,(selas/avg_sales)*0.7,(pview/avg_pview)*0.3+(selas/avg_sales)*0.7
from (select a.ptype, a.pname, a.pview, ifnull(b.sales, 0) as selas
      from test a
               left join test_sell b on a.pid = b.id
      order by a.ptype desc, a.pview DESC) a,
     (
         select ptype, sum(pview) / count(*) as avg_pview from test group by ptype) b,
     (select ptype,round(sum(selas) / count(*), 1) as avg_sales
      from (select a.ptype, a.pname, a.pview, ifnull(b.sales, 0) as selas
            from test a
                     left join test_sell b on a.pid = b.id
            order by a.ptype desc, a.pview DESC) a
      where a.selas > 0
      group by ptype) c

where a.ptype = b.ptype
  and b.ptype = c.ptype order by a.ptype;

总结:通过点击率和销售量的权重,得出商品的欢迎度, 

转载于:https://www.cnblogs.com/8013-cmf/p/11164476.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值