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
Loading
SenthilkumarPosted Mar 12, 2012, 12:02 AM
saasPosted Oct 4, 2012, 3:48 AM
I can display the entries in SQL server that I want to include in this purge date range via the following select statement:
SELECT * FROM tblAddress
WHERE createdDate >= (DATEADD(mm,-6,GETDATE())) AND createdDate <= GETDATE()
I need to modify the below statement which currently searches all entries in the database to delete any duplicate records that have been added to the database (the whole database), I now need to include an extra clause to only include and remove records in the database within the last 6 months.
Previous statement to remove any duplicates, that needs to be modified for new date range call for removing duplicate records:
DELETE FROM tblAddress WHERE A_Id
NOT IN (SELECT MIN(A_Id) FROM tblAddress GROUP BY colAddress)
SELECT * FROM tblAddress ORDER BY createdDate ASC
Jignesh TrivediPosted Mar 12, 2012, 12:21 AM
I have one quesion on your post,
How you identify this is duplicate record (give me some logic to create syntex)?