Solved

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

Posted on 2011-03-24
8
207 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 

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 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

707 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