Avatar of Larry Brister
Larry Brister
Flag 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

Microsoft SQL ServerRemote AccessSQL

Avatar of undefined
Last Comment
Larry Brister

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Pawan Kumar

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Larry Brister

ASKER
Perfect!  Thanks
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck