我有以下查询:
SELECT DISTINCT t.productid as Prod_id,
EXTRACT(week from t.dt) as WEEK,
COUNT(pv.productid) as PageViews,
COUNT(t.productid) as orders,
COUNT(t.productid)/COUNT(DISTINCT(pv.loadid)) as Conversion,
AVG(t.price) avg_price,
MAX(pv.numreviews) max_numreviews,
MIN(pv.numreviews) min_numreviews,
AVG(pv.numreviews) avg_numreviews,
COUNT( DISTINCT pv.bvsid ) sessions
FROM PageView pv LEFT OUTER JOIN Transaction t ON t.productid = pv.productid
AND t.dt BETWEEN '2014-06-06' AND '2014-06-18' AND ( lower(t.currency) = 'usd' OR lower(t.country) IN ('us', 'usa', 'united states') )
WHERE pv.dt BETWEEN '2014-06-06' AND '2014-06-18'
AND pv.client ='abc' AND lower(pv.type) = 'product'
GROUP BY WEEK, Prod_id
ORDER BY WEEK asc;
它为 PageViews 和 Orders 提供相同的值,基本上只采用常见的 productid。我想要页面浏览量和订单的正确值,非常感谢这方面的帮助。
最佳答案
表达式:
COUNT(pv.productid) as PageViews,
COUNT(t.productid) as orders,
统计两个字段的非NULL值。如果它们相同,则所有记录都匹配。但是,我怀疑您想要:
COUNT(pv.productid) as PageViews,
COUNT(distinct t.productid) as orders,
此外,当您有 group by
子句时,您不应使用 select distinct
。
关于sql - 左连接充当内部连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24709049/