Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 newbie question

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-03-05 : 09:01:49
gman writes "updating a table from another table here's some code

UPDATE Orders
SET TotalSales =
(SELECT UnitPrice, Quantity FROM OrderDetails
WHERE Orders.OrderID = OrderDetails.OrderID
AND OrderDetails.UnitPrice * OrderDetails.Quantity)

any help would be greatly appreciated......gman

using SQL 2000"

izaltsman
A custom title

1139 Posts

Posted - 2002-03-05 : 09:16:10
Not quite sure what you are trying to do, but I'm guessing you just want to calculate order total and stick it into the Orders table. So your query will look something like this:


UPDATE o
SET o.TotalSales = od.ttl
FROM Orders o
INNER JOIN (SELECT OrderID, SUM(UnitPrice*Quantity) as ttl
FROM OrderDetails
GROUP BY OrderID) od
ON o.OrderID = od.OrderID


Go to Top of Page
   

- Advertisement -