Link to home
Start Free TrialLog in
Avatar of Larry Brister
Larry BristerFlag for United States of America

asked on

MS SQL cleaner or simplified

My code below works.

It gets the ID and row number
Places  into a #temp table
Gets rit of rn=1
And updates my table.

But... is there a more efficient way to do this?

IF OBJECT_ID('tempdb..#temp') IS NOT NULL
    DROP TABLE #temp;


SELECT   AccommodationsID ,
         ROW_NUMBER() OVER ( PARTITION BY ProspectID ,
                                          TourID
                             ORDER BY AccommodationsID DESC ) rn
INTO     #temp
FROM     dbo.everyware_t_accommodations
WHERE    User10 <> 'Delete'
ORDER BY AccommodationsID DESC;


DELETE FROM #temp
WHERE rn = 1;

UPDATE a
SET    a.User10 = 'Delete'
FROM   everyware_t_accommodations a
       JOIN #temp t ON a.AccommodationsID = t.AccommodationsID
WHERE  1 = 1;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Larry Brister

ASKER

Perfect!  Thanks