Solved

SQL stored proceedure to sum detail records and update a master record

Posted on 2008-10-14
4
246 Views
Last Modified: 2012-05-05
I have a table with transaction details in it. I've also got a table with a master record in it that contains a field for original amount and amount remaining.. I would like to write a stored procedure that will sum the detail records, subtract them from the original amount, and update the master record.

Below is a sample of what I'd like the two tables to look like. Any help would be appreciated.

tblTransactions.

BLOCKID QTY
1                10
1                20
2                5
2                2

tblMaster

BLOCKID  ORIGAMT REMAMT
1               50                 20
2               50                 43



0
Comment
Question by:iansmcg
  • 2
4 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22716209
you mean:
UPDATE m
    SET REMAMT = m.ORIGAMT - ISNULL( t.qty , 0 )
  FROM tblMaster m 
  LEFT JOIN ( SELECT BLOCKID, SUM(QTY) qty FROM tblTransactions group by blockid ) t

Open in new window

0
 

Author Comment

by:iansmcg
ID: 22716276
Well when I attempt to run the following (updated for case and with proper field and table names)

UPDATE m
    SET REAMT = m.ORGAMT - ISNULL(t.QTY , 0 )
  FROM PPtblBlock m
  LEFT JOIN (SELECT BLOCKID, SUM(QTY) QTY FROM PPtblTransactions group by BLOCKID) t

I get:

Server: Msg 102, Level 15, State 1, Line 4
Incorrect syntax near 't'.

0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 22716313
sorry, the last line got "cut"
UPDATE m
    SET REAMT = m.ORGAMT - ISNULL(t.QTY , 0 )
  FROM PPtblBlock m
  LEFT JOIN (SELECT BLOCKID, SUM(QTY) QTY FROM PPtblTransactions group by BLOCKID) t
  ON t.BLOCKID = m.BLOCKID

Open in new window

0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 22716322
Hello iansmcg,

You have to specify which columns the tables are joined on, e.g.:

UPDATE m
    SET REAMT = m.ORGAMT - ISNULL(t.QTY , 0 )
  FROM PPtblBlock m
  LEFT JOIN (SELECT BLOCKID, SUM(QTY) QTY FROM PPtblTransactions group by BLOCKID) t ON m.SomeColumn = t.BLOCKID

Regards,

Patrick
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

808 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