Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.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 - Fatal编程技术网

在sql server中选择重复列

在sql server中选择重复列,sql,sql-server,Sql,Sql Server,表1 Id Name 1 xxxxx 1 ccccc 2 uuuuu 3 ddddd 我想选择Id具有相同Id 如何执行此操作?您可以找到具有多个条目的ids,然后使用左连接/非空模式从原始表中检索相应的数据: SELECT t1.* FROM tbl t1 LEFT JOIN ( SELECT id FROM tbl GROUP BY id HAVING COUNT(*)

表1

Id    Name
1     xxxxx
1     ccccc
2     uuuuu
3     ddddd
我想选择
Id
具有相同
Id


如何执行此操作?

您可以找到具有多个条目的
id
s,然后使用
左连接
/
非空
模式从原始表中检索相应的数据:

SELECT t1.* 
FROM tbl t1 
LEFT JOIN ( SELECT id 
            FROM tbl 
            GROUP BY id 
            HAVING COUNT(*) > 1) t2 ON t1.id = t2.id 
WHERE t2.id IS NOT NULL
其他可能的解决方案包括使用
EXISTS
IN
子句代替
LEFT JOIN
/
不为空

Y as (
select *, count(*) over (partition by id) counter
from X)
select id, name from Y where counter > 1