[Webinar] Learn how to a build a cloud-first strategyRegister Now


Update table field with sum of other table field

Posted on 2004-11-04
Medium Priority
Last Modified: 2009-12-16
I am using SQL Server 2000.

I have a table called tblPartners and I want to run an sp every night that updates a field called TotalPaid.

The table that holds payment info is called tblPaymentInstalments and the field is called InstalmentAmnt.

The 2 tables are related via 2 other tables as follows:

tblPaymentInstalments inner join
tblPayments on tblPaymentInstalments.PaymentRef = tblPayments.PaymentRef inner join
tblProposals on tblPayments.ProposalNo = tblProposals.ProposalNo inner join
tblPartners on tblProposals.PartnerID = tblPartners.PartnerID

How do I write the sp that will update tblPartners.TotalPaid with the sum(tblPaymentInstalments.InstalmentAmnt) for each Partner?

Question by:naqayya
LVL 50

Accepted Solution

Lowfatspread earned 800 total points
ID: 12497117
 from tblpartners as p
 inner Join (select pro.partnerID,sum(pi.InstallmentAmnt) as amt
                   from  tblPaymentInstalments as pi
                  inner join tblPayments as P
                     on PI.PaymentRef = P.PaymentRef
                  inner join tblProposals as Pro
                     on P.ProposalNo = Pro.ProposalNo
                   group by pro.partnerID
                   having sum(pi.InstallmentAmnt) <> 0
     on U.PartnerID = P.PartnerID


Author Comment

ID: 12525342
Thanks Lowfatspread, that worked beautifully!

(you forgot the 'as u' after subquery bracket, but no probs)

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.
Suggested Courses

867 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