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 |
opeterson
Starting Member
1 Post |
Posted - 2009-10-02 : 11:32:14
|
I have a query, building the below temporary table:SearchResultID (PK) - ContentID - Rank1 2134 22 2 2134 1083 2666 664 2666 695 4567 46 4567 123From this table, I want to remove the duplicate ContentID values, leaving only the one with the highest Rank value - and I will then use the PK of SearchResultID in another delete query.So after this action, the above table will look like:SearchResultID (PK) - ContentID - Rank2 2134 1084 2666 696 4567 123This was originally done with cursors which was too slow, way too slow. I tried with a While loop, but again was too slow.Can anyone suggest the fastest way in SQL 2000 to achieve this result? |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-10-02 : 11:38:41
|
do like thisDELETE tFROM table tLEFT JOIN (SELECT ContentID,MAX([Rank]) AS Latest FROM table GROUP BY ContentID)t1ON t1.ContentID=t.ContentIDAND t1.Latest=t.[Rank]WHERE t1.ContentID IS NULL make sure you first do select and make sure the returning ones are real ones to be deleted |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-10-03 : 04:11:51
|
<<make sure you first do select and make sure the returning ones are real ones to be deleted>>I usually advise this for complex deletionsMadhivananFailing to plan is Planning to fail |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-10-03 : 14:37:34
|
quote: Originally posted by madhivanan <<make sure you first do select and make sure the returning ones are real ones to be deleted>>I usually advise this for complex deletionsMadhivananFailing to plan is Planning to fail
I wanted OP to make sure his posted sample data reflected real scenario at hand |
|
|
|
|
|