Solved

How would you delete records with the same Account, DocumentDate and MailID?

Posted on 2011-03-24
8
202 Views
Last Modified: 2012-05-11
I am developing an Access application using Access 2003 with an ADP type file.

I use the following SQL statement to eliminate records with the same Account and Document Date but a different MailID:

delete
from dbo.tblGetDocumentAttributes
where exists (select 1 from dbo.tblGetDocumentAttributes B
                      where B.Account = A.Account
                      and B.DocumentDate = A.DocumentDate                    
                      and B.MailID < A.MailID)

How would you modify this SQL statement or if need be, create a new SQL statement to ALSO delete duplicate records, that is records with the SAME value for all 3 fields, Account, DocumentDate and MailID? See records 4 and 5 below as an example of DUPE records.
----------------------------------------------------------------------------

     MailID   Account   DocumentDate                     ScanDate    Amount.
 1   A34R   45677      2010-08-31 00:00:00.000
 2   B78E    45677      2010-09-30 00:00:00.000
 3   R546    45677      2011-01-31 00:00:00.000
 4   G555    45677      2011-01-31 00:00:00.000
 5   G555    45677      2011-01-31 00:00:00.000
 6   C645    86453      2010-08-31 00:00:00.000
 7   E567    86453      2010-09-30 00:00:00.000
 8   R546    86543      2011-01-31 00:00:00.000
 9   R555    86543      2011-02-28 00:00:00.000


My goal was to delete any records from this table in which
any particular account has multiple records with the same documentdate and in these instances just keep the record with the higher MailID (type nvarchar) ?

So in the example above, the 4th record would be deleted because it has the same
account plus documentdate as record 3 but record 4 has the lower ranking MailID.
.
0
Comment
Question by:zimmer9
  • 3
  • 3
  • 2
8 Comments
 
LVL 32

Expert Comment

by:ewangoya
ID: 35212167

try with CTE

with CTE
as
(
   Select MailID, ROW_NUMBER() OVER (PARTITION BY MailID,  Account,  DocumentDate ORDER BY MailID DESC) ROWNO
   FROM tblGetDocumentAttributes
)  

delete
from tblGetDocumentAttributes
where MailID in (select MailID from CTE where ROWNO > 1)
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 35212180

Disregard, That may not work since the MailID will be duplicate anyway and all the records will be deleted with that mail id
Do you have a primary key in the table
0
 
LVL 32

Accepted Solution

by:
ewangoya earned 300 total points
ID: 35212186

If you have a unique primary key say ID, then

with CTE
as
(
   Select ID, MailID, ROW_NUMBER() OVER (PARTITION BY MailID,  Account,  DocumentDate ORDER BY MailID DESC) ROWNO
   FROM tblGetDocumentAttributes
)  

delete
from tblGetDocumentAttributes
where ID in (select ID from CTE where ROWNO > 1)
0
 

Author Comment

by:zimmer9
ID: 35212189
I think this delete all the records
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 40

Expert Comment

by:Sharath
ID: 35212206
The question is in SQL Server 2005 zone and you said you are working on Access 2003. Can you confirm your database?
0
 

Author Comment

by:zimmer9
ID: 35212362
I am uising Access 2003 for the front end and a SQL Server datebase for the back end.
0
 

Author Comment

by:zimmer9
ID: 35212465
Unfortunately I don't have a primary key.
0
 
LVL 40

Assisted Solution

by:Sharath
Sharath earned 200 total points
ID: 35212751
you can try this.
;with cte as (
select *,row_number() over (partition by Account,DocumentDate order by MailID desc) rn
  from tblGetDocumentAttributes) 
delete from cte where rn > 1

Open in new window

0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Stored procedure query with if 27 36
Isolation level in SQL server 3 47
Error when saving to sql table a '/' 5 26
Insert from Stored Procedure where some field/s > 0 7 43
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now