简体   繁体   English

T-SQL删除一半没有主键的重复项

[英]T-SQL Delete half of duplicates with no primary key

In a T-SQL stored procedure I have a complex procedure that is comparing data using temp tables but at the end of everything when I return a single table I end up with duplicate rows. 在T-SQL存储过程中,我有一个复杂的过程,即使用临时表比较数据,但是当我返回单个表时,最终会出现重复的行。 In these rows all columns in the row are EXACTLY the same and there is no primary key within this table. 在这些行中,行中的所有列都完全相同,并且此表中没有主键。 I need to delete only half of these based on the number of times that row occurs. 我需要根据行发生的次数仅删除其中的一半。 For example if there are eight rows that are all the same value. 例如,如果有八行都是相同的值。 I want to delete four of them. 我想删除其中的四个。

There is no way to get rid of them through my SP filtering because the data that is entered is literally duplicate information entered in by the user but I do required half of that information. 没有办法通过我的SP过滤摆脱它们,因为输入的数据实际上是用户输入的重复信息,但我确实需要一半的信息。

I've done some research on the subject and did some testing but it seems as if it's not possible to delete half of the duplicated rows. 我已经对这个主题进行了一些研究,并进行了一些测试,但似乎不可能删除一半重复的行。 Is this not possible? 这不可能吗? Or is there a way? 或者有办法吗?

Here is one way, using a great feature of SQL Server, updatable CTEs: 这是一种方法,使用SQL Server的一个很棒的功能,可更新的CTE:

with todelete as (
      select t.*,
             row_number() over (partition by col1, col2, col3, . . . order by newid()) as seqnum
      from table t
     )
delete from todelete
    where seqnum % 2 = 0;

This will delete every other value. 这将删除所有其他值。

Assuming SQL Server 2005+: 假设SQL Server 2005+:

;WITH CTE AS
(
    SELECT  *,
            RN=ROW_NUMBER() OVER(PARTITION BY Col1, Col2,...Coln ORDER BY Col1)
    FROM YourTempTableHere
)
DELETE FROM CTE
WHERE RN = 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM