COUNT(DISTINCT X) MySQL 查询没有给出我需要的结果

发布于 2024-12-07 16:47:39 字数 1035 浏览 0 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终遇你 2024-12-14 16:47:39

如果这样做:

SELECT
    Browser,
    day_of_the_week,
    month_of_the_year,
    COUNT(DISTINCT IPAddress)
FROM page_views
WHERE PageID = 58
GROUP BY Browser, day_of_the_week,month_of_the_year

count(distinct) 的总数将与您的简单查询中的不同。

如果你想在同一天消除重复的 IP 地址,你需要这样的东西:

SELECT 
  p1.Browser
  , p1.pageID
  , p1.WEEKDAY(`date`) as day_of_week
  , p1.MONTH(`date`) as The_month
  , COUNT(p2.Unique_visitors_this_day) as uniqueviews
FROM page_views p1
LEFT JOIN (SELECT id, 1 as Unique_visitors_this_day FROM page_views pv1
           LEFT JOIN page_views pv2 ON (pv1.id > pv2.id 
                                     AND pv1.ipaddress = pv2.ipaddress
                                     AND pv1.`date` = pv2.`date`)
           WHERE pv2.id IS NULL) as p2
       ON (p1.id = p2.id)
 WHERE p1.PageID = 58
 GROUP BY p1.Browser, p1.day_of_week, p1.The_month;

If you do:

SELECT
    Browser,
    day_of_the_week,
    month_of_the_year,
    COUNT(DISTINCT IPAddress)
FROM page_views
WHERE PageID = 58
GROUP BY Browser, day_of_the_week,month_of_the_year

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:

SELECT 
  p1.Browser
  , p1.pageID
  , p1.WEEKDAY(`date`) as day_of_week
  , p1.MONTH(`date`) as The_month
  , COUNT(p2.Unique_visitors_this_day) as uniqueviews
FROM page_views p1
LEFT JOIN (SELECT id, 1 as Unique_visitors_this_day FROM page_views pv1
           LEFT JOIN page_views pv2 ON (pv1.id > pv2.id 
                                     AND pv1.ipaddress = pv2.ipaddress
                                     AND pv1.`date` = pv2.`date`)
           WHERE pv2.id IS NULL) as p2
       ON (p1.id = p2.id)
 WHERE p1.PageID = 58
 GROUP BY p1.Browser, p1.day_of_week, p1.The_month;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文