我在SQL Server 2012中有一个表,我想找到从同一IP登录的用户,例如,在我的表中
Id UserName IpAddress
--------------------------------
1 test 192.168.0.1
2 test1 192.168.0.5
3 test3 192.168.0.1
4 test4 192.168.0.5
5 test 192.168.0.15
6 test5 192.168.0.25
7 test 192.168.0.1
8 test 192.168.0.1
9 test 192.168.0.1
10 test3 192.168.0.1
11 test3 192.168.0.1
12 test 192.168.0.1
... .... ...........
在我的查询中
SELECT Id, UserName, IpAddress
FROM (
SELECT Id, UserName, IpAddress,
COUNT(IpAddress) OVER (PARTITION BY IpAddress) AS cnt
FROM UserLogin) AS t
WHERE t.cnt > 1
我得到了结果
1 test 192.168.0.1
2 test 192.168.0.1
3 test 192.168.0.1
4 test 192.168.0.1
5 test 192.168.0.1
6 test3 192.168.0.1
7 test3 192.168.0.1
8 test3 192.168.0.1
9 test 192.168.0.1
.. .... ...
我想得到这个结果
1 test 192.168.0.1 -- user test and user test 3 use one ip
2 test3 192.168.0.1 -- user test and user test 3 use one ip
3 test1 192.168.0.5 -- user test1 and user test 4 use one ip
4 test4 192.168.0.5 -- user test1 and user test 4 use one ip
.. .... ...
我想获得使用相同IP的玩家
我怎么做?
谢谢
答案 0 :(得分:3)
您希望拥有多个用户的IP。这是一种方法:
SELECT UserName, IpAddress
FROM (SELECT UserName, IpAddress,
COUNT(*) OVER (PARTITION BY IpAddress) as cnt
FROM UserLogin
GROUP BY UserName, IpAddress
) ui
WHERE cnt > 1
ORDER BY IpAddress;