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:使用Distinct、Not Exists、CTE和Union选择重复值_Sql Server_Duplicates_Union_Common Table Expression_Not Exists - Fatal编程技术网

Sql server SQL:使用Distinct、Not Exists、CTE和Union选择重复值

Sql server SQL:使用Distinct、Not Exists、CTE和Union选择重复值,sql-server,duplicates,union,common-table-expression,not-exists,Sql Server,Duplicates,Union,Common Table Expression,Not Exists,我需要从如下所示的对象表中选择以绿色突出显示的重复值: 我尝试了下面代码的不同组合。但无法返回两个重复的行 ;with CTE as (Select distinct ID, count([Object ID]) as [Object ID] from #Object group by ID having count([Object ID]) > 1) select * from CTE where NOT EXISTS (Select distinct

我需要从如下所示的对象表中选择以绿色突出显示的重复值:

我尝试了下面代码的不同组合。但无法返回两个重复的行

;with CTE as
    (Select distinct ID, count([Object ID]) as [Object ID] 
from #Object 
     group by ID having count([Object ID]) > 1)

select * from CTE where 
    NOT EXISTS (Select distinct ID , count(distinct [Object ID]) as [Object ID] 
from #Object group by ID having count(distinct [Object ID]) > 1);
Declare @YourTable table (ID int,ObjectID int,ObjectName varchar(50))
Insert into @YourTable values
(250708,321,'hotel'),
(250708,343,'mercantile'),
(250708,370,'parking'),
(250708,370,'residential condominium'),
(250708,370,'residential condominium'),
(250708,401,'residential condominium'),
(250708,401,'residential condominium')



;with cte as (
    Select *
          ,RN = Row_Number() over ( Partition By ID,ObjectID,ObjectName Order by (Select NULL))
     From  @YourTable
)
Select * 
 From cte 
 Where RN>1

可以使用窗口函数ROW_NUMBER()来标识重复的行

;with CTE as
    (Select distinct ID, count([Object ID]) as [Object ID] 
from #Object 
     group by ID having count([Object ID]) > 1)

select * from CTE where 
    NOT EXISTS (Select distinct ID , count(distinct [Object ID]) as [Object ID] 
from #Object group by ID having count(distinct [Object ID]) > 1);
Declare @YourTable table (ID int,ObjectID int,ObjectName varchar(50))
Insert into @YourTable values
(250708,321,'hotel'),
(250708,343,'mercantile'),
(250708,370,'parking'),
(250708,370,'residential condominium'),
(250708,370,'residential condominium'),
(250708,401,'residential condominium'),
(250708,401,'residential condominium')



;with cte as (
    Select *
          ,RN = Row_Number() over ( Partition By ID,ObjectID,ObjectName Order by (Select NULL))
     From  @YourTable
)
Select * 
 From cte 
 Where RN>1
返回


另一方面,您可以通过将最后的
Select*
替换为
delete

来删除这些记录。下面是第一个答案中代码的正确版本

;With cte as (
        Select *
              ,RN = Row_Number() over (Partition By ID, [Object ID] Order by (Select NULL))
       From tblObject 
     )

    Select *
     From cte 
     Where RN>1;

这将丢失任何一组副本中的第一行。@MartinSmith仅显示第二个副本highlighed@MartinSmith令人鼓舞的是,知道这种情况可能发生在你的级别和声誉的人身上。:)John,你必须去掉NULL后面的括号,并在“YourTable”中放置两个。John,当我解析代码时,它也会说:Command(s)completed successfully。但当我实际执行时,它说:1。必须指定要从中选择的表。无效的列名称。。。。2.列名“ID”无效,列名“对象ID”无效。。。3.无效的列名“对象名”,4。没有为“cte”的第1列指定列名。做得好+我只是对未来的一个暗示。如果您将示例数据发布为文本(可以复制和粘贴,而不是图像),这将更有帮助。通过这种方式,我们可以测试SQL是否有合适的字段名和/或语法,