SQL Looping

I'm quite new to T-SQL, so bear with me.

I have a table with columns UserName, PCName, Date.  There is no key field and there may be many entries from the same user name.  I only want to keep the 10 newest entries for each unique user name.  I've written code like below:

DELETE FROM dbo.PCs WHERE UserName = 'MyUserName' AND 
Date NOT IN (SELECT TOP 10 Date FROM dbo.PCs WHERE UserName = 'MyUserName' ORDER BY Date DESC)

Open in new window


This works fine but how do I expand it and run it for each unique user.  I can isolate the unique users easily enough with: SELECT DISTINCT UserName FROM dbo.PCs , but how do I loop through the result.

I do understand that looping isn't a favored approach in SQL, but I'm also having trouble figuring our how to do this any other way.  I'm not quite grasping JOINs and such.  I'm certainly open to alternatives to looping, but it may take some explaining.
LVL 10
nashiookaAsked:
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.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Eyeballeth thy article on SQL Server Delete Duplicate Rows Solutions, scroll down to the 'Delete #1 section', and change the query to meet your needs and your definition of '10 newest entries', then change WHERE row_number > 1 to > 10.
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
I can't understand your question. Why do you want an unique user?
0
PortletPaulEE Topic AdvisorCommented:
always backup tables before whacking them with untested code where data could be lost
& for a delete query I suggest trying it as a select query first
;WITH cte
AS (
    SELECT
        UserName
      , PCName
      , [Date]
      , ROW_NUMBER() OVER (PARTITION BY UserName, PCName ORDER BY [Date] DESC) AS rn
)
SELECT * FROM cte
WHERE rn > 10
;

Open in new window


If that looks reasonable then:
;WITH cte
AS (
    SELECT
        UserName
      , PCName
      , [Date]
      , ROW_NUMBER() OVER (PARTITION BY UserName, PCName ORDER BY [Date] DESC) AS rn
)
DELETE FROM cte
WHERE rn > 10
;

Open in new window


note that DESC(ending) order is needed to keep the 10 latest records.

also:
no key on a table = not good
using "reserved words" as a column name = not great; use [Date]  to identify that field clearly
0

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
nashiookaAuthor Commented:
Sorry this took so long.  I kind of got distracted off the SQL learning path.  The selected solution however was exactly what I needed.  Thanks it was enlightening.
0
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

From novice to tech pro — start learning today.