Delete all records except the latestest record of a group

How would I delete all records from a table (TABLEA) except the latest record (by Action Date) for that customer number ?


Before:

Custno      ActionDate      ActionNote
F050908A      2015-09-30      A/R System value change
F123088W      2015-10-06      A/R System value change
F123088W      2015-10-05      A/R System value change
F123088W      2015-10-02      A/R System value change
F123088W      2015-10-01      A/R System value change
F123088W      2015-09-30      A/R System value change
F123090W      2015-09-30      A/R System value change
F263605A      2015-10-06      A/R System value  change
F263605A      2015-10-05      A/R System value change
F263605A      2015-10-02      A/R System value change
F263605A      2015-10-01      A/R System value change
F263605A      2015-09-30      A/R System value change
F493020D      2015-10-06      A/R System value change
F493020D      2015-10-05      A/R System value change

After:

Custno      ActionDate      ActionNote
F050908A      2015-09-30      A/R System value change
F123088W      2015-10-06      A/R System value change
F123090W      2015-09-30      A/R System value change
F263605A      2015-10-06      A/R System value change
F493020D      2015-10-06      A/R System value change
thayduckProgrammer AnalystAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
Use a CTE and the ROW_NUMBER() window function:

WITH    Ordered
          AS ( SELECT   * ,
                        ROW_NUMBER() OVER ( PARTITION BY Custno ORDER BY ActionDate DESC ) AS RN
               FROM     yourTable
             )
    DELETE  FROM Ordered
    WHERE   RN <> 1;

Open in new window


And as always: Check your backups first.

btw, "latest record" - we call them rows - implies that (Custno, ActionDate) is unique. When this is not the case, take a look at the RANK() window function.

For testing purposes you can use the CTE and do a SELECT * FROM Ordered WHERE RN <> 1 to see the affected rows.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.