Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
| Search | Today's Posts | Mark Forums Read
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 5th, 2004, 06:40 AM
Authorized User
 
Join Date: Mar 2004
Location: , , .
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default Update parent table with the sum of child table

Hi all

COuld anyone help with this one because its driving me nuts!!

I have a parent table that contains an order number and a total order value

The child table to this has order number, part code, qty and cost each.

What I am trying to do is write an SQL Update that sums all the child records and puts the value into the parent record so something like

update order_header set total_order_value=(select sum(item_qty*cost_each) from child where parent.order_number=child_order_number)

but this gives a syntax error in SQL Server.

Any help would be gratefully received.

Regards
Graham


 
Old November 9th, 2004, 07:35 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Bangalore, KA, India.
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Graham,

As per your description, I see only two tables involved in this. But where does the ORDER_HEADER table come from which you are trying to update. Can you explain that bit clearer? What is the error that it gives? can you post that too?

_________________________
- Vijay G
Strive for Perfection
 
Old November 9th, 2004, 07:53 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Bangalore, KA, India.
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Graham,

I suggest you use a cursor to do this as you cannot do that with just one update statement.
Code:
select order_number, sum(item_qty*cost_each) from child group by order_number order by order_number
This would result in what you are looking for. May be use can use INTO clause there in the above select statement to store it in to a separate table. Else go for a cursor based solution. That should be the right one.

Hope that helps.
Cheers!

_________________________
- Vijay G
Strive for Perfection




Similar Threads
Thread Thread Starter Forum Replies Last Post
update table from another db table timbal25 SQL Server 2005 3 January 19th, 2008 06:47 AM
Insert Into Parent Child Table prasanta2expert Access VBA 3 November 8th, 2007 11:18 AM
Update one table to another table using DTS in SQL Printmaker SQL Language 0 July 24th, 2007 07:17 AM
update table with an ID Num from different table scoobie PHP How-To 12 January 25th, 2005 12:28 PM
Parent and Child Records in Same Table owain SQL Language 12 October 27th, 2004 10:02 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.