Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there,

I have setup a C# application which saves data to a SQL database. I have a button in the application that checks the database for any duplicate records then removes the latest record, by checking the datetime stamp that is added to each record when they are saves to the SQL database.
I now have a great number of records within the database and would like to modify the DELETE statement, which as mentioned previously: this checks all records in the database and removes the duplicate record with the latest datetime stamp.
I now need to modify this statement to only check the last 6 months of records and to ignore if there is any duplicates older than six months.
I have the following SELECT statement which does return the values that I would like the DELETE statement to focus on:
SQL
WHERE createdDate >= (DATEADD(mm,-6,GETDATE())) AND createdDate <= GETDATE()

This is the previous statement that I had setup for removing duplicates from the database, but this checks all records in the database, I need to include a date range into this statement.
SQL
DELETE FROM tblAddress
WHERE A_Id NOT IN (SELECT MIN(A_Id) FROM tblAddress GROUP BY colAddress)
SELECT * FROM tblAddress ORDER BY createdDate ASC

The SQL database includes the following Columns for each record saved to the database:
A_Id = Auto generated ID number, int, PK
colAddress = varChar(255) used to store address
createdDate = datetime, used to track when the record was added to the database.

I need to include this date range into the pre-existing statement under the WHERE clause for removing records, if there is a duplicate record that is older, but within the 6 month range from now:
Current statement to be altered:
SQL
DELETE FROM tblAddress 
WHERE A_Id NOT IN (SELECT MIN(A_Id) FROM tblAddress GROUP BY colAddress)

date range that needs to be included in the WHERE clause:
SQL
WHERE createdDate >= (DATEADD(mm,-6,GETDATE())) AND createdDate <= GETDATE()
Posted
Updated 4-Oct-12 17:30pm
v4
Comments
Rockstar_ 5-Oct-12 0:51am    
For this problem yo have to use the concept of Partition...

Find the pseudo code - Add your own date time difefrence logic

SQL
DELETE FROM Table WHERE A_Id NOT IN
( 
SELECT MAX(A_Id) FROM table WHERE createdDate > 6 months
GROUP BY Id 

)
 
Share this answer
 
v3
Hi,

Use the below query.

SQL
DELETE FROM tblAddress WHERE A_Id NOT IN (SELECT MIN(A_Id) FROM tblAddress GROUP BY colAddress) AND createdDate >= (DATEADD(mm,-6,GETDATE());

Hope this will work.

Then check the result again
SELECT * FROM tblAddress ORDER BY createdDate
 
Share this answer
 
v2
Comments
[no name] 5-Oct-12 3:01am    
Hi Mohd,
I have composed the following statement which includes both of the conditions I require.

DELETE FROM tblAddress WHERE A_Id NOT IN (SELECT MIN(A_Id) FROM tblAddress GROUP BY colAddress) AND createdDate >= (DATEADD(mm,-6,GETDATE()))

When testing this it does select the correct range (within 6 months of now), but the actual DELETE statement will still check for entries earlier than 6 months for a duplicate, instead of just checking for duplicates within the records for the last 6 months.
I do not want to check for duplicate records that are older than 6 months, only want to check for duplicates within the last 6 months.
Mohd. Mukhtar 5-Oct-12 4:05am    
See the below ans. That might help you.
Ok
Just interchange the condition.

SQL
DELETE FROM tblAddress WHERE createdDate >= (DATEADD(mm,-6,GETDATE())) AND A_Id NOT IN (SELECT MIN(A_Id) FROM tblAddress GROUP BY colAddress);

As the SQL statement execute from left to right, Hence it will check createdDate condition first and then execute other entries, But any how you don't need to check the earlier entries, So the other option is check date condition 2 time as below query.

SQL
DELETE FROM tblAddress WHERE createdDate >= (DATEADD(mm,-6,GETDATE()) AND A_Id NOT IN (SELECT MIN(A_Id) FROM tblAddress WHERE createdDate >= (DATEADD(mm,-6,GETDATE())) GROUP BY colAddress);
 
Share this answer
 
v4
Comments
[no name] 7-Oct-12 21:33pm    
That's done it. Thank you Mohd :-) Perfect

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900