我试图找到在线系统用户使用的ip /端口组合的数量。我想创建具有每个用户名使用的最大ips数和端口数的列。
我开始的基本查询是:
Select distinct username, ip, port from users u, peers p where u.usernum=p.usernum group by username, ip, port
对于数据:
a 1.1.1.1 12345 a 1.1.1.1 13579 b 9.8.7.6 11111 b 9.5.5.5 11115 b 9.2.2.2 11111 c 5.6.7.8 33333 c 5.6.0.0 33333
我希望输出类似于以下内容:
user max_ip max_port a 1 2 b 3 2 c 2 1
我尝试了各种形式的分组,但没有成功。在此先感谢您的帮助。
答案 0 :(得分:3)
SELECT username
, COUNT(DISTINCT ip) AS max_ip
, COUNT(DISTINCT port) AS max_port
FROM users u
JOIN peers p
ON u.usernum = p.usernum
GROUP BY username
答案 1 :(得分:2)
SELECT username,COUNT(DISTINCT ip) AS max_ip,COUNT(DISTINCT port) AS max_port
FROM users ur, peers pr where ur.usernum = pr.usernum
GROUP BY username
答案 2 :(得分:0)
尝试类似:
Select username, count(ip), count(distinct port)
from users u, peers p where u.usernum=p.usernum group by username
答案 3 :(得分:0)
以下查询将解决您的问题:
Select username, COUNT( distinct ip), COUNT( distinct port) from users group by username