Using SQL Server 2005 I have a query that gets child records from bundles that has a calculated field for quantity. The query looks something like this:
SELECT TblB_1.fooID,
TblC.quantity * (TblA.quantity) AS Quantity,
TblB_1.name AS Name
FROM TblB AS TblB_1 INNER JOIN
TblC ON TblB_1.fooID = TblC.fooID RIGHT OUTER JOIN
TblB INNER JOIN
TblA ON TblB.fooID = TblA.fooID ON TblC.parentfooID = TblB.fooID
WHERE (TblB.isBundle = 1) AND (TblA.isDeleted = 0)
I need to be able to group by id and get a SUM of the quantity field. I’ve tried wrapping the quantity line with sum like this:
SUM(TblC.quantity * (TblA.quantity)) AS Quantity,
Then adding
GROUP BY TblB_1.fooID
after the WHERE clause, but that results in a “TblB_1.name is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause” error.
I also tried to get my head around using a subquery for this task, but I was not able to make that work either, nor could I find an example here or on the Web that I have been able to adapt. Thanks – Dan
You were close, you need to group by all the non aggregate fields. Like so