Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Database > SQL Language
Password Reminder
Register
| FAQ | Members List | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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


Reply With Quote
  #2 (permalink)  
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
Reply With Quote
  #3 (permalink)  
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 02:30 PM.


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