?
Solved

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

Posted on 2011-03-24
8
Medium Priority
?
209 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 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

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

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

752 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