我有日期和时间条目。我希望结果按小时 (00, 01, 02) 分组,这很有效,但是当我想获得不同的用户数时,出现错误。
Select Substr(time, 0, 2) as Hour,
Count(date) as Hits,
Count(Distinct ip) as Users,
Count(Distinct X-Forwarded-For) as ForwardedUsers
From table
Group By Hour
编辑:
我使用的是 Microsoft 的 LogParser,我可以按原样使用 Group By Hour
,X-Forwarded-For
也没有问题。
问题是我如何在组内使用 Count(Distinct ip)
最佳答案
不幸的是,LogParser 不支持 DISTINCT
聚合函数和 GROUP BY
。从上面查询得到的错误消息中应该可以清楚地看到这一点:
Error: Semantic Error: aggregate functions with DISTINCT arguments are not supported with GROUP BY clauses
你可以做的一个技巧是完全删除 GROUP BY
子句并改为计算 COUNT DISTINCT(hourlyIp)
其中 hourlyIp
是一个字符串将小时与 IP 地址连接起来。然后,在处理结果时,您必须将 hourlyIp
字段分解回其组件。
关于sql - Count(Distinct x) 和 Group By y,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46054785/