sql server - SQL Query - Delete duplicates if more than 3 dups? -


Is there an elegant SQL statement for deleting duplicate records in a table, but only if X is the number of duplicates? So it allows up to 2 or 3 duplicates, but what is it?

At present I have a statement that does the following:

  Remove table from table, left outer (duplicate from table group like Triad, Diplo 1, Duplex 2) 1, Select Max (ID) by Dupl 2. Maintenance as TDI) = Maintenance. Where are kept. Road is empty  

This works very well. But what I want to do now will only remove those lines, if they have more than 2 duplicates.

Thanks

 with  in the form of CT (select row_member ( )) (Divol1 by split, dupol2 sequence by id) from table as rn) from cte where rn & gt; 2; - or & gt; 3 etc.  

The query is being classified by a 'line number' for each record, (Dupl 1, Dupl 2) and ordered by ID. In fact, this line number is calculated as 'duplicate', which has the same duplicate 1 and duplex 2 and then specifies the number 1, 2, 3. If you want to keep just 2 'duplicates', then you have to delete those numbers which were assigned 3,4, .. N and that DELLETE .. where RNA & gt ; 2;

Using this method you can change ORDER BY to suit your preferred order (like ORDER BY ID DESC ), So that latest has rn = 1 , next to the latest = rn = 2 and so on. The rest remains the same, DELETE will only remove old people because they have the highest line number

Unlike, as the condition becomes more complex, CTE and Row_Number () Is easy to use. If there is no appropriate access index, then the display can still be problematic.


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 -