Sql 重复值
因此,我试图从两个表Transaction和PagViews中获取页面视图和订单。运行此查询时:Sql 重复值,sql,database,postgresql,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 Pa
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,数据如下所示:
productid周页面浏览订单平均评论
78431968110
基本上,pageview值应该是88,而不是第1周的968
我确实了解到,它正在对相同的数据进行11次计数
有人能解释一下我在这个问题上做错了什么,或者遗漏了什么吗。谢谢如果您正在寻找页面浏览量与订单数量的比率,那么下面的内容应该可以做到
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 ;
谢谢,但那不是我想要的。我只是想知道为什么页面浏览量值如此之高,为什么数据会重复。@AshwinSingh,因为计数是按选定的所有记录而不是一行进行的。您有11个订单行和968个产品行。哦,好的,那么我应该使用count distinct,对吗?count distinct将为您提供唯一值的计数。如果您将从订单中删除distinct,您将得到968。@AshwinSingh和88是什么?每周浏览量?