Solved

SQL Update query

Posted on 2014-11-05
9
371 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
Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

 

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 49

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

Webinar: MongoDB® Index Types

Join Percona’s Senior Technical Services Engineer, Adamo Tonete as he presents “MongoDB Index Types, How, When and Where Should They be Used?” on Wednesday, July 12, 2017 at 11:00 am PDT / 2:00 pm EDT (UTC-7).

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

717 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