SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Selecting/Deleting rows with the same column information


Selecting/Deleting rows with the same column information

Author
Message
hogpen
hogpen
SSC-Enthusiastic
SSC-Enthusiastic (139 reputation)SSC-Enthusiastic (139 reputation)SSC-Enthusiastic (139 reputation)SSC-Enthusiastic (139 reputation)SSC-Enthusiastic (139 reputation)SSC-Enthusiastic (139 reputation)SSC-Enthusiastic (139 reputation)SSC-Enthusiastic (139 reputation)

Group: General Forum Members
Points: 139 Visits: 323
I need to delete some rows from our system that have the same transaction id based on an accounting date. Our primary key for the table is MtrNo, MtrSfx, TrnxID, RecCode.

Here's some example data:
ProdDate | AcctDate | MtrNo |MtrSfx | TrnxID | RecCode
------------------------------------------------------
2/1/2011 | 8/1/2011 | 365 |A | 8301 | RR
2/1/2011 | 2/1/2013 | 365 |A | 8301 | RR
2/1/2011 | 2/1/2011 | 365 |A | 8301 | OR
3/1/2011 | 9/1/2011 | 365 |A | 8302 | RR
3/1/2011 | 3/1/2013 | 365 |A | 8302 | RR
3/1/2011 | 3/1/2011 | 365 |A | 8302 | OR


I want to be able to delete all the most recent rows based on AcctDate that have an RR RecCode. So in the above table, I'd want to delete:
2/1/2011 | 2/1/2013 | 365 |A | 8301 | RR
3/1/2011 | 3/1/2013 | 365 |A | 8302 | RR

It seems to be simple but I'm not very good with T-SQL. I would assume I need a sub-query that uses max, group by, and count.
Would something like the query below work?

DELETE FROM Tbl
WHERE (SELECT MtrNo, MtrSfx, TrnxID, RecCode, MAX(AcctDate)
FROM Tbl
WHERE RecCode = 'RR'
GROUP BY MtrNo, MtrSfx, TrnxID, RecCode
HAVING COUNT(TrnxID) > 1)


The select seems to give me the right results but I don't know how the delete statement works and if it would just delete the columns I bring back.

Any help would be greatly appreciated!
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)

Group: General Forum Members
Points: 67559 Visits: 18570
Here is a quick article on how to dedupe data

http://jasonbrimhall.info/2011/03/21/dedupe-data-cte/



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

ChrisM@home
ChrisM@home
SSCertifiable
SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)

Group: General Forum Members
Points: 5298 Visits: 10608
Like this?


Low-hanging fruit picker and defender of the moggies





For better assistance in answering your questions, please read this.




Understanding and using APPLY, (I) and (II) Paul White

Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)

Group: General Forum Members
Points: 67559 Visits: 18570
ChrisM@home (4/26/2013)
Like this?


A few of them today eh?



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

Calibear
Calibear
SSC-Addicted
SSC-Addicted (455 reputation)SSC-Addicted (455 reputation)SSC-Addicted (455 reputation)SSC-Addicted (455 reputation)SSC-Addicted (455 reputation)SSC-Addicted (455 reputation)SSC-Addicted (455 reputation)SSC-Addicted (455 reputation)

Group: General Forum Members
Points: 455 Visits: 132
Hogpen,

Next time it would be helpful to present the data like this, then we could easily duplicate the data and write the queries:

CREATE TABLE Accounting
(ProdDate DateTime,
AcctDate DateTime,
MtrNo int,
MtrSfx char(1),
TrnxID int,
RecCode char (2)
)

insert into Accounting
select '2/1/2011','8/1/2011','365','A','8301','RR' union all
select '2/1/2011','2/1/2013','365','A','8301','RR' union all
select '2/1/2011','2/1/2011','365','A','8301','OR' union all
select '3/1/2011','9/1/2011','365','A','8302','RR' union all
select '3/1/2011','3/1/2013','365','A','8302','RR' union all
select '3/1/2011','3/1/2011','365','A','8302','OR'

Here is the delete statement that I've come up with:

DELETE Accounting
FROM Accounting a,
(SELECT MtrNo, MtrSfx, TrnxID, RecCode, MAX(AcctDate) AcctDate
FROM Accounting
WHERE RecCode = 'RR'
GROUP BY MtrNo, MtrSfx, TrnxID, RecCode
HAVING COUNT(TrnxID) > 1) b
where a.MtrNo = b.MtrNo and a.MtrSfx = b.MtrSfx
and a.TrnxID = b.TrnxID and a.RecCode = b.RecCode
and a.AcctDate = b.AcctDate

You need to group the data by all the primary key fields and then join that data on the original table and select. I find it is always helpful to write my code as a delete and just before I delete it, run it as a select to make sure I have the right code:

select a.*
FROM Accounting a,
(SELECT MtrNo, MtrSfx, TrnxID, RecCode, MAX(AcctDate) AcctDate
FROM Accounting
WHERE RecCode = 'RR'
GROUP BY MtrNo, MtrSfx, TrnxID, RecCode
HAVING COUNT(TrnxID) > 1) b
where a.MtrNo = b.MtrNo and a.MtrSfx = b.MtrSfx
and a.TrnxID = b.TrnxID and a.RecCode = b.RecCode
and a.AcctDate = b.AcctDate

--------
For SQL Tips, check out my blog: http://lantztechknowledge.blogspot.com/
You can also follow my twitter account to get daily updates: @BLantz2455
hogpen
hogpen
SSC-Enthusiastic
SSC-Enthusiastic (139 reputation)SSC-Enthusiastic (139 reputation)SSC-Enthusiastic (139 reputation)SSC-Enthusiastic (139 reputation)SSC-Enthusiastic (139 reputation)SSC-Enthusiastic (139 reputation)SSC-Enthusiastic (139 reputation)SSC-Enthusiastic (139 reputation)

Group: General Forum Members
Points: 139 Visits: 323
Thank you all for your help! Sorry for the data issue.
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)

Group: General Forum Members
Points: 67559 Visits: 18570
hogpen (4/30/2013)
Thank you all for your help! Sorry for the data issue.


Were you able to get it resolved?



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

hogpen
hogpen
SSC-Enthusiastic
SSC-Enthusiastic (139 reputation)SSC-Enthusiastic (139 reputation)SSC-Enthusiastic (139 reputation)SSC-Enthusiastic (139 reputation)SSC-Enthusiastic (139 reputation)SSC-Enthusiastic (139 reputation)SSC-Enthusiastic (139 reputation)SSC-Enthusiastic (139 reputation)

Group: General Forum Members
Points: 139 Visits: 323
Yes, thank you again!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search