Waterstone
asked on
Need to update parent fields with AVG of child fields
Hi,
I need a query to update a parent table with the average of related child fields from all child records.
Tables: ParentTable, ChildTable
ParentTable.AVGCost1 = AVG(ChildTable.Cost1)
ParentTable.AVGCost2 = AVG(ChildTable.Cost2)
ParentTable.AVGCost3 = AVG(ChildTable.Cost3)
Joined on ParentTable.ParentTableKey = ChildTable.ParentTableKey
Would like to use a stored proc and pass it ParentTableKey
Thanks
I need a query to update a parent table with the average of related child fields from all child records.
Tables: ParentTable, ChildTable
ParentTable.AVGCost1 = AVG(ChildTable.Cost1)
ParentTable.AVGCost2 = AVG(ChildTable.Cost2)
ParentTable.AVGCost3 = AVG(ChildTable.Cost3)
Joined on ParentTable.ParentTableKey
Would like to use a stored proc and pass it ParentTableKey
Thanks
ASKER
Thank you. Seems to work well. Can you show me the query to follow this suggestion?
'For more than one record you can use IN instead of equality in where clauses (make sure you put WHEREs in both queries, one in derived table will limit amount of processing that needs to be done, and the main one - records updated.'
I've done similar things in MS SQL but it was long ago.
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you! Works great and the examples you provided will help me in many ways.
Open in new window
This will be quite a heavy query as it will process the whole ChildTable.
More selective version working on a single ParentTableKey value (you can also make it a subquery), I assume that __value is a declared store proc variable.
Open in new window
For more than one record you can use IN instead of equality in where clauses (make sure you put WHEREs in both queries, one in derived table will limit amount of processing that needs to be done, and the main one - records updated.