COUNT(DISTINCT X) MySQL 查询没有给出我需要的结果
在 MySQL 中,我有一个查询来统计我想要聚合的 page_views 表中的所有浏览器。这是我的查询:
SELECT
Browser,
COUNT(*)
FROM page_views
WHERE PageID = 58
GROUP BY Browser;
例如,它给出:
Chrome 14 Firefox 17 Internet Explorer 9 Opera 5 Safari 3 -------------------- Total 48
这正是我想要的。它工作得很好而且速度很快。现在我想对 IP 地址进行区分,以删除多次查看该页面的用户。我在 COUNT
中添加了 DISTINCT
,所以它看起来像这样:
SELECT
Browser,
COUNT(DISTINCT IPAddress)
FROM page_views
WHERE PageID = 58
GROUP BY Browser;
这看起来可行,但实际上,我认为它不起作用。在我的现实环境中,我对多列进行聚合,一列用于浏览器,一列用于一周中的某一天,一列用于月份等...因此,每个列都是一个查询,但具有不同的GROUP BY
。我注意到这可能不是我想要的结果,因为我的每个查询都有不同的总数。按浏览器聚合视图可得出 48 个总视图,按星期汇总可得出 45 个总视图,按月份可得出 50 个总视图。有什么不对劲。
另外,如果我执行一个简单的 SELECT COUNT(DISTINCT IPAddress) FROM page_views WHERE PageID = 58 ,我得到的页面浏览量比其他聚合查询的总浏览量要少得多。
同样,我想要的是上面放置的第一个查询,但只为每个 IP 地址聚合一次,因此它基本上计算页面的唯一查看者,而不是页面的浏览总数。
我在做什么或没有正确地做才能得到我想要的结果?
谢谢。
In MySQL I have a query to count all of the browsers from a page_views table that I want to aggregate. Here's my query:
SELECT
Browser,
COUNT(*)
FROM page_views
WHERE PageID = 58
GROUP BY Browser;
which gives, for example:
Chrome 14 Firefox 17 Internet Explorer 9 Opera 5 Safari 3 -------------------- Total 48
This is exactly what I want. It works fine and it's fast. Now I wanted to to a distinct on the IP address to remove users that have viewed the page more than once. I added DISTINCT
in the COUNT
so it looks like this:
SELECT
Browser,
COUNT(DISTINCT IPAddress)
FROM page_views
WHERE PageID = 58
GROUP BY Browser;
This looks like it works but in reality, I don't think it does. In my real-world environment, I do aggregates on multiple columns, one for Browser, on for day of the week, one for the month etc... so it's one query each but with different GROUP BY
. How I noticed that it may not be the result I want is that every one of my queries has a different number for totals. Aggregating the views by browsers gives 48 total views, by day of week gives 45 total views, by month gives 50 total views. Something's not right.
Also, if I do a simple SELECT COUNT(DISTINCT IPAddress) FROM page_views WHERE PageID = 58
I get much less page views that the other aggregate queries have as a total.
Again, what I want is the first query I put above but only aggregate once for each IPAddress so it basically calculates the unique viewers of a page and not the total of views a page has.
What am I doing, or not doing correctly to get the results I want?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果这样做:
count(distinct)
的总数将与您的简单查询中的不同。如果你想在同一天消除重复的 IP 地址,你需要这样的东西:
If you do:
The total of
count(distinct)
will no be the same as in your simple query.If you want to eliminate duplicate IP-adresses in the same day, you'll need something like this: