Solved

SQL Update query

Posted on 2014-11-05
9
354 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
  • 4
  • 4
9 Comments
 
LVL 15

Expert Comment

by:Haris Djulic
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:gfk76
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 15

Expert Comment

by:Haris Djulic
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks Paul ;)
0
 

Author Closing Comment

by:gfk76
Comment Utility
Thank you very much for solution
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
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…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now