How to remove duplicate data from SQL keeping the oldest record
Hi There,
I have a C# application that saves data to a SQL database. I have a button in the application that deletes all duplicate data from the SQL database, although each time it is keeping the latest record rather than keeping the oldest record.
When the data is saved to the database it saves the timedate that the record has been saved to the database.
Here is my SQL Command to remove the duplicate data records from the database for the Purge Duplicates button event:
SqlCommand frsCommand = new SqlCommand("DELETE FROM tblAddress WHERE A_Id NOT IN (SELECT MAX(A_Id) FROM tblAddress GROUP BY colAddress)SELECT * FROM tblAddress ORDER BY createdDate ASC", frsCon);
How can I amend this code to remove the latest records and keep the oldest record in the database?
My database table contains the following Columns:
A_Id (auto generated ID number, int, Primary Key)
colAddress (varChar(255) used to store address)
createdDate (datetime, used to track when the record was added to the database)
Please help :-)
Thank you