How can I update a SQL table with a numeric counter based on different values in a column

I have a table which contains items for my customers and I need to update each row with a counter that will tell me if this is the first item they purchased, the 5th or the 20th.

My table looks like this (Example)


CustomerID, transactionID, ItemCounter
29248, 5678953, NULL
29248, 5679526, NULL
29248, 5679863, NULL
86136, 6897856, NULL
86136, 6897956, NULL
86136, 6898113, NULL
86136, 6898569, NULL
16253, 3586214, NULL
16253, 3698874, NULL
16253, 3792365, NULL

After the update I'd like the table to look like this

CustomerID, transactionID, ItemCounter
29248, 5678953, 1
29248, 5679526, 2
29248, 5679863, 3
86136, 6897856, 1
86136, 6897956, 2
86136, 6898113, 3
86136, 6898569, 4
16253, 3586214, 1
16253, 3698874, 2
16253, 3792365, 3
16253, 3798956, 4
16253, 3804569, 5

As you can see the "transactionid" counts up for each customer. I could also add columns such as a "transaction date" to help me order the items by customerid,  but the "transactionid" seems to be enough of a determiner.

Assume the best way to do this is using a CURSOR , but I'm not sure how to crawl through the table and do the "increment" based on the transactionid. The table above is an example and the real table will have 100's of "customers" with multiple "transactions" between them so it will contain tens of thousands of rows based on the time frame being searched. The table is actually a #temptable which is created during a stored procedure used to generate a report so speed of updating the table is important. The #temptable is also the result of selecting data from other tables into it. Just wanted to give that background . Thanks
Who is Participating?

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

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.

Scott PletcherSenior DBACommented:
SET ItemCounter = tn_ic.ItemCounter
FROM table_name tn
    SELECT CustomerID, transactionID,
        ROW_NUMBER() OVER(PARTITION BY CustomerID ORDER BY transactionID) AS ItemCounter
    FROM table_name
) AS tn_ic ON
    tn_ic.CustomerID = tn.CustomerID AND
    tn_ic.transactionID = tn.transactionID

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
svasilakosAuthor Commented:
Wow. Assume!!   Thanks for that quick response. Solution worked perfectly for me and was just what I wanted. Nice work!!  

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.