T-SQL Delete half of duplicates with no primary key

Mickey Sly

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. 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.

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?

Gordon Linoff

Here is one way, using a great feature of SQL Server, updatable CTEs:

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

delete duplicates using pig where there is no primary key

From Dev

Update Duplicates in SQL Server without a primary key

From Dev

can't delete foreign and primary key

From Dev

SQL Delete duplicate rows in the table without primary key on SQL Server

From Dev

i want to delete all duplicates in a column without creating another table, there is no primary key,no index and no other constraint in the table

From Dev

Delete duplicate sql entries based on distinct Primary Key

From Dev

Can't delete primary key on table with FULLTEXT index

From Dev

Delete model by primary key in SQLAlchemy

From Dev

Update primary key - if duplicate, then delete it

From Dev

Update primary key - if duplicate, then delete it

From Dev

Removing duplicates based on systemID, primary key and email

From Dev

SQL Primary Key Generation

From Dev

SQL Primary Key Decisions

From Dev

SQL Primary Key reset?

From Dev

SQL Primary key struggle

From Dev

SQL Primary Key Exception

From Dev

SQL Primary Key Decisions

From Dev

SQL - Tuple as Primary Key

From Dev

SQL Primary Key Increment

From Dev

T-SQL Unique Identity Column as Part of Composite Primary Key

From Dev

Insert column to make the primary key in T-SQL

From Dev

t-sql 2012 update foreign key value in primary table

From Dev

T-SQL stored procedure exception due to primary key constraint

From Dev

Delete foreign key with primary key reference

From Dev

SQL Server: composite key primary key constraint fk, can't understand a SQL schema diagram

From Dev

Delete duplicates based on Group By - SQL

From Dev

Dependency rule tried to blank-out primary key column in SQL-Alchemy when trying to delete record

From Dev

Primary key design in SQL Server

From Dev

SQL Primary Key Duplicate Values

Related Related

  1. 1

    delete duplicates using pig where there is no primary key

  2. 2

    Update Duplicates in SQL Server without a primary key

  3. 3

    can't delete foreign and primary key

  4. 4

    SQL Delete duplicate rows in the table without primary key on SQL Server

  5. 5

    i want to delete all duplicates in a column without creating another table, there is no primary key,no index and no other constraint in the table

  6. 6

    Delete duplicate sql entries based on distinct Primary Key

  7. 7

    Can't delete primary key on table with FULLTEXT index

  8. 8

    Delete model by primary key in SQLAlchemy

  9. 9

    Update primary key - if duplicate, then delete it

  10. 10

    Update primary key - if duplicate, then delete it

  11. 11

    Removing duplicates based on systemID, primary key and email

  12. 12

    SQL Primary Key Generation

  13. 13

    SQL Primary Key Decisions

  14. 14

    SQL Primary Key reset?

  15. 15

    SQL Primary key struggle

  16. 16

    SQL Primary Key Exception

  17. 17

    SQL Primary Key Decisions

  18. 18

    SQL - Tuple as Primary Key

  19. 19

    SQL Primary Key Increment

  20. 20

    T-SQL Unique Identity Column as Part of Composite Primary Key

  21. 21

    Insert column to make the primary key in T-SQL

  22. 22

    t-sql 2012 update foreign key value in primary table

  23. 23

    T-SQL stored procedure exception due to primary key constraint

  24. 24

    Delete foreign key with primary key reference

  25. 25

    SQL Server: composite key primary key constraint fk, can't understand a SQL schema diagram

  26. 26

    Delete duplicates based on Group By - SQL

  27. 27

    Dependency rule tried to blank-out primary key column in SQL-Alchemy when trying to delete record

  28. 28

    Primary key design in SQL Server

  29. 29

    SQL Primary Key Duplicate Values

HotTag

Archive