Jump to content

Adding a calculated column to a table

- - - - -

  • Please log in to reply
No replies to this topic

#1
reachpradeep

reachpradeep

    Learning Programmer

  • Members
  • PipPipPip
  • 41 posts
Having a problem with adding data to a column based on a calculated result from a second table. I seem to get the same result for all rows. It needs to be either an update or insert statement. It works as a select but that is about it. Here is the code. I need is to add a column to customers which I call Sales that takes the result from Orders and OrderItems table.
-----------------------
SELECT C.CustomerID, SUM(OI.Qty * OI.Price) AS Sales
FROM OrderItems OI
INNER JOIN Orders O
ON OI.InvoiceID = O.InvoiceID
INNER JOIN Customers C
ON O.CustomerID = C.CustomerID
AND O.CustomerID = C.CustomerID
INNER JOIN Products P
ON OI.ProductID = P.ProductID
GROUP BY C.CustomerID, C.CustomerID

Select * from Customers
-------------------------------




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users