Solved

SQL Update query

Posted on 2014-11-05
9
363 Views
Last Modified: 2014-11-05
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.

PS!
English is not my primary language, but hopefully you understand what I need.
0
Comment
Question by:gfk76
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 40423585
try this :

UPDATE t1
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

0
 

Author Comment

by:gfk76
ID: 40423601
Thank you for taking time to answer.

However I get the error Message "table t1 not found" Line 1
0
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 40423639
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
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

Author Comment

by:gfk76
ID: 40423647
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
0
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 40423659
run this query:

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

and post output
0
 

Author Comment

by:gfk76
ID: 40423693
This is the output

customerid,value
950273,1
467962,1
337128,1

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

I get
customerid,count()
950273,3
467962,4
337128,4
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 40423710
I think you need row_number(), please try:

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

Open in new window

0
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 40423729
Thanks Paul ;)
0
 

Author Closing Comment

by:gfk76
ID: 40423738
Thank you very much for solution
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have a large data set and a SSIS package. How can I load this file in multi threading?
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how the fundamental information of how to create a table.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question