Solved

Synchronize a Master column with its detail totals

Posted on 2006-11-06
5
308 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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
This works though you have to add SELECT to the beginning of the subqueries

thanks again,
arw
0
 
LVL 142

Expert Comment

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

743 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

18 Experts available now in Live!

Get 1:1 Help Now