重复的价值观

时间:2014-07-24 16:15:00

标签: sql database postgresql

所以我试图从两个表Transaction和PagViews中获取网页浏览量和订单。当我运行此查询时:

   EXPLAIN
   SELECT 
   pv.productid,
   EXTRACT(week from pv.dt) as WEEK,
   COUNT(pv.productid) as PageViews,
   COUNT(distinct t.orderid) as Orders, 
   AVG(pv.numreviews) avg_numreviews
   FROM PageView pv 
   LEFT OUTER JOIN Transaction t 
   ON pv.productid = t.productid 
   AND t.client ='xyz'
   WHERE 
   pv.client ='xyz' 
   AND pv.dt BETWEEN '2014-01-01' AND '2014-06-30'
   GROUP BY pv.productid, WEEK
   ORDER BY WEEK asc ;

我得到的价值很少,但对于某些产品,数据是这样的:

productid WEEK PageView Orders Avg。的评价

7843 1 968 11 0

基本上,网页浏览量值应为88,而不是第1周的968。

我确实知道它正在计算11次相同的数据!

有人可以了解我在这个查询中做错了什么或者缺少什么。感谢

1 个答案:

答案 0 :(得分:0)

如果您正在寻找PageViewsOrders的比率,那么应该遵循以下规则。

SELECT 
   pv.productid,
   EXTRACT(week from pv.dt) as WEEK,
   COUNT(pv.productid) as PageViews,
   (COUNT(pv.productid) / COUNT(distinct t.orderid)) as PageViewsDividedByOrderCount,
   COUNT(distinct t.orderid) as Orders, 
   AVG(pv.numreviews) avg_numreviews
   FROM PageView pv 
   LEFT OUTER JOIN Transaction t 
   ON pv.productid = t.productid 
   AND t.client ='xyz'
   WHERE 
   pv.client ='xyz' 
   AND pv.dt BETWEEN '2014-01-01' AND '2014-06-30'
   GROUP BY pv.productid, WEEK
   ORDER BY WEEK asc ;