Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.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 Server,查找基于列的重复行_Sql_Sql Server_Select - Fatal编程技术网

SQL Server,查找基于列的重复行

SQL Server,查找基于列的重复行,sql,sql-server,select,Sql,Sql Server,Select,假设我有一个数据表,如下所示: ID Name Birthday ------ -------- ------------ 01 Eric 06/20 02 Judy 03/15 03 Mary 04/01 04 John 03/15 05 Judy 06/20 06

假设我有一个数据表,如下所示:

  ID        Name         Birthday
------    --------     ------------
  01        Eric          06/20
  02        Judy          03/15
  03        Mary          04/01
  04        John          03/15
  05        Judy          06/20
  06        John          09/11
如何根据名称获得结果:

  ID        Name         Birthday
------    --------     ------------
  02        Judy          03/15
  04        John          03/15
  05        Judy          06/20
  06        John          09/11
因为朱迪和约翰都出现过不止一次(重复)

基于生日:

  ID        Name         Birthday
------    --------     ------------
  01        Eric          06/20
  02        Judy          03/15
  04        John          03/15
  05        Judy          06/20

因为06/20和03/15都出现过不止一次

您应该在SQL server中查找WHERE、GROUP BY子句以及内部查询和联接。 以下是指向MSDN文档的链接

使用GROUP BY并在何处查询案例1:

Select * from Tbl where Name in 
(
Select Name
from Tbl
Group by Name
HAVING COUNT(1)>1
)
类似地,第二个结果应该如下所示:

Select * from Tbl where Birthday in 
(
Select Birthday
from Tbl
Group by Birthday
HAVING COUNT(1)>1
)
基于名称

select * from myTable where name in(
select Name from myTable group by Name having count(*)>1)
你可以在那上面为鸟日换些零钱

这是SQLFIDLE链接,您可以在其中根据实际数据库更改表名或字段数据类型

为什么不存在使用


要根据
生日
获取日期,请在内部查询中使用
生日

目前还不清楚是哪种逻辑产生了输出表。请澄清它已更新!
select * 
from table t
where exists (select 1 
              from table 
              where Name = t.name 
              having count(1) > 1
             );