Click to See Complete Forum and Search --> : Using SUM with an inner join


yamaharuss
02-18-2009, 09:56 AM
I'm trying to get a total sum of the following items

Item1 has a qty of 2 with price of $100
Item2 has a qty of 1 with price of $1

So the SUM I am looking for from my query would be $201 but my query is only returning $200. It's not grabbing the next record row


SELECT OI.OrderItemID,OI.ItemID,OI.Name,OI.ItemCode,OI.Options,OI.OptionsPrice,OI.ItemPrice,OI.Quantity, I.VendorID, Sum(ItemPrice*Quantity) AS iSubtotal FROM C_OrderItems OI INNER JOIN C_Items I ON I.ItemID = OI.ItemID WHERE OrderID=11682 GROUP By OI.OrderItemID,OI.ItemID,OI.Name,OI.ItemCode,OI.Options,OI.OptionsPrice,OI.ItemPrice,OI.Quantity, I.VendorID

What am I missing?

mintedjo
02-18-2009, 10:03 AM
What does the group by do?

yamaharuss
02-18-2009, 10:09 AM
Without the group clause I get:

Microsoft OLE DB Provider for SQL Server (0x80040E14)
Column 'OI.OrderItemID' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.

chazzy
02-18-2009, 05:02 PM
you need to take OrderItemID out of both the group by and select clauses. you can't select it because each row will have its own OrderItemID but to get 201 you have to ignore the OrderItemID.