Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
HGClubTec
Starting Member
12 Posts |
Posted - 2010-09-17 : 11:09:24
|
I have a table that sporatically gets duplicates and causes problems. My goal is complete a delete query that I can run that will delete the newest of the entries. So here is my current query...selectID,RevenueCodeID,TaxIDfromctcreceivables.dbo.revenuecodetaxxref RWhere R.RevenueCodeID IN (Select RevenueCodeID from ctcreceivables.dbo.RevenueCodeTaxXREF X Group By X.RevenueCodeID, X.TaxID HAVING Count(RevenueCodeID) > 1 AND Count(TaxID) > 1)Order By RevenueCodeID,IDAnd my results are:ID RevenueCodeID TaxID55 351 656 351 657 351 658 351 684 359 6114 359 6I'd like to delete ID's 56, 57, 58 & 114.I feel like I'm close to at least extracting the data I'd like to delete, but I cant't figure out the last part.TIA for any help I get!! |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2010-09-17 : 11:24:32
|
[code]-- Sample DataDECLARE @T TABLE (ID INT, RevenueCodeID INT, TaxID INT)INSERT @TVALUES(55, 351, 6),(56, 351, 6),(57, 351, 6),(58, 351, 6),(84, 359, 6),(114, 359, 6)-- AS Derived TableDELETE TFROM ( SELECT * ,ROW_NUMBER() OVER (PARTITION BY RevenueCodeID, TaxID ORDER BY ID) AS RowNum FROM @T ) AS TWHERE RowNum > 1 -- Or using a CTE;WITH cteRevAS( SELECT * ,ROW_NUMBER() OVER (PARTITION BY RevenueCodeID, TaxID ORDER BY ID) AS RowNum FROM @T )DELETE cteRevWHERE RowNum > 1[/code] |
 |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2010-09-17 : 11:30:36
|
Or for fun as a join:DELETE TFROM @T AS TINNER JOIN ( Select MIN(ID) AS ID, RevenueCodeID, TaxID from @T AS X Group By X.RevenueCodeID, X.TaxID HAVING COUNT(*) > 1 ) AS D ON T.RevenueCodeID = D.RevenueCodeID AND T.TaxID = D.TaxIDWHERE T.ID <> D.ID |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-09-17 : 11:58:34
|
quote: Originally posted by Lamprey
-- Sample DataDECLARE @T TABLE (ID INT, RevenueCodeID INT, TaxID INT)INSERT @TVALUES(55, 351, 6),(56, 351, 6),(57, 351, 6),(58, 351, 6),(84, 359, 6),(114, 359, 6)-- AS Derived TableDELETE TFROM ( SELECT * ,ROW_NUMBER() OVER (PARTITION BY RevenueCodeID, TaxID ORDER BY ID) AS RowNum FROM @T ) AS TWHERE RowNum > 1 -- Or using a CTE;WITH cteRevAS( SELECT * ,ROW_NUMBER() OVER (PARTITION BY RevenueCodeID, TaxID ORDER BY ID) AS RowNum FROM @T )DELETE cteRevWHERE RowNum > 1
actually you dont need that * in inner query. you just need to return only rownum from inner query for deletion------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2010-09-17 : 12:18:11
|
That is true. I tend to leave it in there for the forums to make it easier for people to run just the select and see what data it reutns. :) |
 |
|
|
|
|
|
|