Solved

How to eliminate duplicate accounts selectively from a SQL Server 2005 table?

Posted on 2011-03-10
3
282 Views
Last Modified: 2012-06-21
I am developing an Access application usng Access 2003,

I use an ADP type file with SQL Server 2005.

I have a table named tblGetDocumentAttributes with fileds including the following:

      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   C645    86453      2010-08-31 00:00:00.000
 6   E567    86453      2010-09-30 00:00:00.000
 7   R546    86543      2011-01-31 00:00:00.000
 8   R555    86543      2011-02-28 00:00:00.000


Can you think of a SQL statement 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 Comments
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 200 total points
ID: 35102543

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

Assisted Solution

by:Aaron Tomosky
Aaron Tomosky earned 150 total points
ID: 35102597
In SQL you can do something like this:
While(exists(select count(*), documentdate from tblGetDocumentAttributes group by documentdate having count(*)>1))
begin
Delete from tblGetDocumentAttributes whare mailid = (select top 1 mailid from tblGetDocumentAttributes group by documentdate having count(*)>1 order by mailid);
End
0
 
LVL 11

Assisted Solution

by:JoeNuvo
JoeNuvo earned 150 total points
ID: 35102614
with SQL 2005
this command should do

;WITH CTE AS (
	SELECT MailID, ROW_NUMBER() OVER (PARTITION BY Account, DocumentDate ORDER BY MailID DESC) RN
	FROM tblGetDocumentAttributes
)
DELETE FROM tblGetDocumentAttributes
WHERE MailID IN
(
	SELECT MailID FROM CTE WHERE RN > 1
)

Open in new window

0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

734 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