Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# SQL
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:
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.
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:
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:
WHERE createdDate >= (DATEADD(mm,-6,GETDATE())) AND createdDate <= GETDATE()
Posted 4-Oct-12 15:50pm
saas207292
Edited 4-Oct-12 17:30pm
v4
Comments
VijayChauhan123 at 5-Oct-12 0:51am
   
For this problem yo have to use the concept of Partition...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

here you get solution
 

check it[^]
heck last point
may be help you..
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Find the pseudo code - Add your own date time difefrence logic
 
DELETE FROM Table WHERE A_Id NOT IN
( 
SELECT MAX(A_Id) FROM table WHERE createdDate > 6 months
GROUP BY Id 
 
)
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi,
 
Use the below query.
 
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
  Permalink  
v2
Comments
saas207 at 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 at 5-Oct-12 4:05am
   
See the below ans. That might help you.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Ok
 
Just interchange the condition.
 
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.
 
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);
  Permalink  
v4
Comments
saas207 at 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)



Advertise | Privacy | Mobile
Web03 | 2.8.140709.1 | Last Updated 5 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid