Solved

Synchronize a Master column with its detail totals

Posted on 2006-11-06
5
315 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
[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
  • 2
  • 2
5 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

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…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

623 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