sql--測試商品的重要度,是否須要及時補貨

表1:商品表sql

表2:商品售賣表spa

需求:算出商品的平均點擊率、平均銷售、商品受歡迎度

1.使用inner join查出每件商品的點擊率和銷售額度3d

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

結果:code

2.查出每一個商品類的平均點擊率blog

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

 

3.查出每一個商品類的平均銷售額度class

     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:test

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;

總結:經過點擊率和銷售量的權重,得出商品的歡迎度, select

相關文章
相關標籤/搜索