I am having issues getting this code to work. Using AdventureWorks2012, my task is to list the order's customer name, order status, date ordered, count of items on the order, and average quantity ordered where the count of items on the order is greater than 300. This is what I have come up with but it won't compile. Any advice?
SELECT soh.CustomerID, soh.Status, soh.OrderDate, COUNT(sod.OrderQty), AVG(sod.OrderQty)
FROM Sales.SalesOrderHeader AS soh
JOIN Sales.SalesOrderDetail AS sod ON soh.SalesOrderID = sod.SalesOrderID
RIGHT JOIN Sales.SalesOrderDetail On soh.SalesOrderID = sod.SalesOrderID
SELECT SalesOrderID, sum(OrderQty) AS 'Total Items'
FROM sales.SalesOrderDetail
GROUP BY SalesOrderID
HAVING sum(OrderQty) > 300;
You probably want to join these two queries that you provided. You are missing the
GROUP BY
clause in the first query. One solution how to put them together is to use IN: