Solved

SQL Update query

Posted on 2014-11-05
9
365 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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

732 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