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
Solved

SQL Update query

Posted on 2014-11-05
9
361 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
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Activity Monitor detail 2 28
sql server query 12 26
SQLCMD Output to file from xpcmd_shell 6 17
Exchange database won't mount 4 21
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

837 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