Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 选择出现3次以上的IP_Sql_Sql Server - Fatal编程技术网

Sql 选择出现3次以上的IP

Sql 选择出现3次以上的IP,sql,sql-server,Sql,Sql Server,如果我有一张这样的桌子 id ip ------ --------- 1 192.212.1.1 2 192.212.1.1 3 156.232.1.1 4 192.212.1.1 5 192.212.1.1 6 561.235.2.1 7 156

如果我有一张这样的桌子

  id             ip
------        ---------
  1              192.212.1.1
  2              192.212.1.1
  3              156.232.1.1
  4              192.212.1.1
  5              192.212.1.1
  6              561.235.2.1
  7              156.261.2.2
到目前为止,我已经尝试过这个查询

SELECT ip FROM voluum_clicks HAVING COUNT(ip) > 3;
我需要它回来的是

  id             ip
------        ---------
  1              192.212.1.1
  2              192.212.1.1
  4              192.212.1.1
  5              192.212.1.1
试试这个:

select 
    id, 
    ip
from voluum_clicks 
where ip in
(
    SELECT 
        ip 
    FROM voluum_clicks 
    GROUP by ip
    HAVING COUNT(1) > 3
);

您可以按如下方式使用计数:

Select Id, [Ip] from (
    Select *, count([id]) over(partition by [Ip]) as RCnt from #ipdata
    ) a Where a.RCnt >= 3

+----+-------------+
| Id |     Ip      |
+----+-------------+
|  1 | 192.212.1.1 |
|  2 | 192.212.1.1 |
|  4 | 192.212.1.1 |
|  5 | 192.212.1.1 |
+----+-------------+
输出如下:

Select Id, [Ip] from (
    Select *, count([id]) over(partition by [Ip]) as RCnt from #ipdata
    ) a Where a.RCnt >= 3

+----+-------------+
| Id |     Ip      |
+----+-------------+
|  1 | 192.212.1.1 |
|  2 | 192.212.1.1 |
|  4 | 192.212.1.1 |
|  5 | 192.212.1.1 |
+----+-------------+

您缺少按ip分组的