Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Synchronize a Master column with its detail totals

Posted on 2006-11-06
5
Medium Priority
?
322 Views
Last Modified: 2008-02-26
UPDATE MASTER SET total = (SUM(detail.balance))
INNER JOIN DETAIL ON detail.ID = master.ID
WHERE master.total <>  (SUM(detail.balance))

I already know the above is incorrect, so here is my question

how would I UPDATE only those MASTER rows that had a total column that did not match the sum of the detail.balance column or would I have to just zero the total column and do something like this->

UPDATE MASTER SET total = total + detail.balance
INNER JOIN DETAIL ON detail.ID = master.ID

thanks,
arw
 
0
Comment
Question by:ARW
  • 2
  • 2
5 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1500 total points
ID: 17882849
UPDATE MASTER SET total = (SUM(detail.balance) FROM detail where  detail.ID = master.ID )
WHERE master.total <>  (SUM(detail.balance) FROM detail where  detail.ID = master.ID )
or master.total is null
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 17882932
Hi Angerl,
 Will it work ? it will give an error i think since u are trying to update using an aggregate function
0
 

Author Comment

by:ARW
ID: 17882951
This works though you have to add SELECT to the beginning of the subqueries

thanks again,
arw
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17883503
indeed, i forgot it in the copy/paste operation:

UPDATE MASTER SET total = (select SUM(detail.balance) FROM detail where  detail.ID = master.ID )
WHERE master.total <>  (select SUM(detail.balance) FROM detail where  detail.ID = master.ID )
or master.total is null

glad I could help, sorry for the missing SELECT...
0
 

Author Comment

by:ARW
ID: 17884024
no problem... the OR in the WHERE clause was something I would have forgot for sure without you having added it.
thanks
~aw
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how the fundamental information of how to create a table.

824 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