I am receiving the following error:
Column 'Products.ProductName' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause
Select P.ProductName , C.City , Sum(OD.Quantity )as TotalSold,
DENSE_RANK() OVER ( PARTITION BY C.CITY ORDER BY OD.Quantity Desc) as rank
from Products P
Inner Join [Order Details] OD on P.ProductID = OD.ProductID
Inner Join Orders O on O.OrderID = OD.OrderID
Inner Join Customers C on C.CustomerID = O.CustomerID
where country = 'USA'
But if use the below query, it gives me the correct data. whats the difference between the two if I'm joining all 4 tables in different order?
select p.productName, c.city, od.quantity,
DENSE_RANK () OVER ( Partition by C.city Order by od.quantity Desc) as rank
from customers C
Inner Join Orders O on C.customerID = O.CustomerID
Inner Join [Order Details] od on O.OrderID = OD.OrderID
Inner Join Products P on OD.ProductID = P.ProductID
where country = 'USA'
group by. - Dale KSum(OD.Quantity)i.e. you are trying to aggregate, to aggregate you need togroup bysomething else there is nothing to aggregate. As I said above, read the docs, they are very clear on how to usegroup by. - Dale KGROUP BY? Whatabiout the error don't you understand? - Larnu