?
Solved

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

Posted on 2011-03-24
8
Medium Priority
?
214 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:Ephraim Wangoya
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:Ephraim Wangoya
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:
Ephraim Wangoya earned 1200 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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 

Author Comment

by:zimmer9
ID: 35212189
I think this delete all the records
0
 
LVL 41

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 41

Assisted Solution

by:Sharath
Sharath earned 800 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

862 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