?
Solved

Synchronize a Master column with its detail totals

Posted on 2006-11-06
5
Medium Priority
?
319 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 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

718 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