SQL Update query

Hi Experts!

I am in need for an sql update query on one of my tables.

My table has a list of invoices, where one customer can have 1 or more invoices.
I am trying to do an update where I add an incremented number per invoice for each customer.
See example below.

This is the table right now

Customerid      invoiceno      Value
1245            1123456            null
1245            1123457            null
1236            1123458            null
1245            1123459            null
1236            1123460            null
1265            1123461            null
9875            1123462            null

The column Value, I would like to have an incremented value per customer,so that my table would look like this:

Customerid      invoiceno      Value
1245            1123456            1
1245            1123457            2
1236            1123458            1
1245            1123459            3
1236            1123460            2
1265            1123461            1
9875            1123462            1

I have tryed to put together an update query, but without any luck.
This is what I have so far, but it only gives me the total for each customer.
I am not sure how to make the value incremented.

UPDATE table t
set value=(select count(*) from table where t.customerid=table.Customerid)
where value is null

Hopefully someone can help me.

English is not my primary language, but hopefully you understand what I need.
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.

Haris DulicIT ArchitectCommented:
try this :

set t1.value=t2.value
from table t1 left join ( select customerid, COUNT(1) OVER (PARTITION BYcustomerid) as value from table
group by customerid
) t2 on t.customerid=t2.customerid
where t1.value is null

Open in new window

gfk76Author Commented:
Thank you for taking time to answer.

However I get the error Message "table t1 not found" Line 1
Haris DulicIT ArchitectCommented:
You need to replace the table with your table name, since you did not post the real table name... Also the column name for value column
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

gfk76Author Commented:
This is my exact Query.
It runs, but it puts value=1 on all invoices. It does not increment

UPDATE Invoices
set t1.value=t2.value
from Invoices t1 left join ( select customerid, COUNT(1) OVER (PARTITION BY customerid) as value from Invoices
group by customerid
) t2 on t1.customerid=t2.customerid
where t1.value is null
Haris DulicIT ArchitectCommented:
run this query:

select customerid, COUNT(1) OVER (PARTITION BY customerid) as value from Invoices
group by customerid

and post output
gfk76Author Commented:
This is the output


when I do
Select customerid,count(*) from invoices
Group by customerid

I get
PortletPaulEE Topic AdvisorCommented:
I think you need row_number(), please try:

UPDATE Invoices
SET t1.value = t2.value
FROM Invoices T1
            , ROW_NUMBER() OVER (PARTITION BY customerid
                                  ORDER BY invoiceno) AS VALUE
            FROM Invoices
            GROUP BY customerid
          ) T2
            ON T1.customerid = T2.customerid

Open in new window


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
Haris DulicIT ArchitectCommented:
Thanks Paul ;)
gfk76Author Commented:
Thank you very much for solution
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
Sybase Database

From novice to tech pro — start learning today.