ASKER
SELECT PKcol, someOthercols
FROM YourTable
WHERE SomeCondition
DELETE FROM YourTable WHERE PKcol IN (
SELECT PKcol
FROM YourTable
WHERE SomeCondition
)
-- or you may leave the original query unchanged
DELETE FROM YourTable WHERE PKcol IN (SELECT PKcol FROM (
SELECT PKcol, someOthercols
FROM YourTable
WHERE SomeCondition
) )
ASKER
ASKER
ASKER
Select col1,col2, col3 FROM maintable WHERE condition
-- becomes
DELETE d FROM maintable d WHERE condition
- but also in more complex queries
Select col1,col2, col3 FROM maintable LEFT JOIN detailtable ON condition -...further joins... WHERE condition
-- becomes
DELETE d FROM maintable d LEFT JOIN detailtable ON condition ...further join... WHERE condition
DELETE FROM MyTable WHERE PKcol IN (SELECT PKcol FROM (
SELECT PKcol, ROW_NUMBER() OVER (PARTITION BY Col1, Col2, Col3 ORDER BY PKcol) rn
FROM MyTable) dupl
WHERE rn > 1 )
Of course you should test it first:
SELECT PKcol, col1, col2, col3, ROW_NUMBER() OVER (PARTITION BY Col1, Col2, Col3 ORDER BY PKcol) rn
FROM MyTable
ORDER BY col1, col2, col3, 5
ASKER
ASKER
ASKER
ASKER
ASKER
SELECT * FROM (
SELECT PKcol, col1, col2, col3, ROW_NUMBER() OVER (PARTITION BY Col1, Col2, Col3 ORDER BY PKcol DESC) rn
FROM MyTable ) allRows
WHERE rn > 1
ORDER BY col1, col2, col3, 5
ASKER
It makes sense to keep the newer record.
Thus, because PK is sequential record numbers, sorting by PK should work?
Thus rn=2 can be deleted in all cases?
SELECT * FROM yourtable Where col1='some specific value' -quotes or not depend on data type.
SQL (Structured Query Language) is designed to be used in conjunction with relational database products as of a means of working with sets of data. SQL consists of data definition, data manipulation, and procedural elements. Its scope includes data insert, query, update and delete, schema creation and modification, and data access control.
TRUSTED BY
Does the SELECT query return the primary key of the records you want to delete?
P.S Have you tried simply changing SELECT to DELETE?