sql - Delete records which are considered duplicates based on same value on a column and keep the newest -


I would like to delete records that are considered a duplicate based on those which have the same value in a certain column and Based on the latest one is considered based on InsertedDate in my example. I want a solution that does not use a cursor, but the set is based. Goal: Delete all duplicates and keep up to date

The DDL below makes some duplicates that the records to be removed are: John1 & amp; John 2 because they have the same identity as John 3 and Johns 3 is the latest record.

In addition to this, join 5 is required to record because there is another record with id = 3 and new (John 6).

  Create a table dibow. Include TestTable (ID id, insert date, date name (50)) in debo. TestTable1, '07 / 01/200 ',' John 1 'in the deboe. Select TestTable1, insert '07 / 02/200 9 ',' John 2 'into the DIBO. Select TestTable1, insert '07 / 03/200 9 ',' John 3 'in Dibbo. Choose TestTable2, '07 / 03/200 9 ',' John 4 'insert in Debo. TestTable 3, '07 / 05/200 9 ',' John 5 'in the deboe. TestTable 3, '07 / 06/200 9 ', select' John 6 ' 

< P> This works:

  Remove the deleted tab from TestTable T (Choose ID, insert = maximum (insert) as sub on sub-id = t.id) According to the TestTable group) and sub.InsertedDate = t.InsertedDate where the sub.id is empty  

If you have to deal with it, a small bit pancreator.


Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -