1

I need a query which will delete all the records from the table which has exactly same row(even same id) but keep only one.

Here is my database structure:

      [ ID      STATUS   ] 
       '1', 'New Request'
       '1', 'New Request'
       '2', 'Old Request'

A query which will delete the first or maybe the second row but keep one record. So, there can be 3 , 4 or maybe more identical records but i have to delete all and keep only one. Please, assist me.

Awais Ahmad
  • 378
  • 1
  • 4
  • 17

2 Answers2

1

Depending on how big your table is, you could copy all distinct records into a temp table, truncate the original table, then copy back into the original:

CREATE TEMPORY TABLE tmp AS SELECT DISTINCT * FROM table;
TRUNCATE table;
INSERT INTO table SELECT * FROM tmp;
Jacob Lambert
  • 7,449
  • 8
  • 27
  • 47
0

Try following set of queries:

Select distinct [id], [status] into temptable from table

Go

Delete from table

Go

Insert into table select [id], [status] from temptable

Go

Drop table temptable

Go