I am trying to get this SQL query to grab 1 more thing from another table. However, when I try and add it to the select, it is giving me an error – “Column ‘OrderItem.orderItemValue’ is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.”
Here is the original SQL query (in a stored procedure) which works fine:
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE SP_InvoiceReports_CustProducts
@custIdent varchar(50) ,
@startDate datetime ,
@endDate datetime
AS
set @endDate = dateAdd(month, 1, @startDate)
SELECT product.styleDesc, product.styleRef , empCust.custName , productSize.sizeChartNme ,
Sum(orderItemAudit.shipQuantity) as theShipment ,
Sum(orderItemAudit.shipValue) as theCost
FROM orderItemAudit INNER JOIN
orders ON
orderItemAudit.orderIdent = orders.orderIdent INNER JOIN
OrderItem ON
orderItemAudit.orderItemIdent = OrderItem.orderItemIdent AND
orders.orderIdent = OrderItem.orderIdent INNER JOIN
empCust ON
orders.custIdent = empCust.custIdent INNER JOIN
product ON OrderItem.styleRef = product.styleRef INNER JOIN
productSize ON OrderItem.sizeChartIdent = productSize.sizeChartIdent
WHERE empCust.custIdent = @custIdent
and orderItemAudit.dateShipped >= @startDate
and orderItemAudit.dateShipped <= @endDate
GROUP BY product.styleRef, product.styleDesc, empCust.custName, productSize.sizeChartNme
ORDER BY product.styleRef, product.styleDesc, empCust.custName, productSize.sizeChartNme
Here it is after I added what I want to select to it:
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE SP_InvoiceReports_CustProducts
@custIdent varchar(50) ,
@startDate datetime ,
@endDate datetime
AS
set @endDate = dateAdd(month, 1, @startDate)
SELECT product.styleDesc, product.styleRef , empCust.custName , productSize.sizeChartNme , OrderItem.orderItemValue ,
Sum(orderItemAudit.shipQuantity) as theShipment ,
Sum(orderItemAudit.shipValue) as theCost
FROM orderItemAudit INNER JOIN
orders ON
orderItemAudit.orderIdent = orders.orderIdent INNER JOIN
OrderItem ON
orderItemAudit.orderItemIdent = OrderItem.orderItemIdent AND
orders.orderIdent = OrderItem.orderIdent INNER JOIN
empCust ON
orders.custIdent = empCust.custIdent INNER JOIN
product ON OrderItem.styleRef = product.styleRef INNER JOIN
productSize ON OrderItem.sizeChartIdent = productSize.sizeChartIdent
WHERE empCust.custIdent = @custIdent
and orderItemAudit.dateShipped >= @startDate
and orderItemAudit.dateShipped <= @endDate
GROUP BY product.styleRef, product.styleDesc, empCust.custName, productSize.sizeChartNme
ORDER BY product.styleRef, product.styleDesc, empCust.custName, productSize.sizeChartNme
I don’t understand this because that table IS in an INNER JOIN?
Any help would be greatly appreciated.
You didnt add the column to the group by expression: