Solved

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

Posted on 2011-03-24
8
201 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future 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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Adventure works database .msi 4 50
count vs exists 9 48
spx for moving values to new table 5 59
How to query LOCK_ESCALATION 4 37
This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now