I am trying to write aquery based on the old Northwind database. My task is to write a query, that will give me "number of units in stock by category and supplier continent". I've wrote something like this:
SELECT Categories.CategoryName as "Categories",
CASE WHEN Suppliers.Country in
('UK','Spain','Sweden','Germany','Norway',
'Denmark','Netherlands','Finland','Italy','France')
THEN 'Europe'
WHEN Suppliers.Country in
('USA', 'Brazil','Canada')
THEN 'America'
ELSE 'Asia-Pacific'
END AS "Supplier Continent",
sum(Products.UnitsInStock) as "Units In Stock"
FROM Suppliers
INNER JOIN Products ON Products.SupplierID=Suppliers.SupplierID
INNER JOIN Categories ON Categories.CategoryID= Products.CategoryID
GROUP BY Categories.CategoryName;
Sadly the MS SQL Server Management Studio throws me an error :
Msg 8120, Level 16, State 1, Line 165
Column 'Suppliers.Country' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
What should I change to fix this error and make my query work?
Suppliers.Countryisn't being aggregated, neither is it in theGROUP BY.GROUP BY Categories.CategoryName, Suppliers.Country;