0

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?

7
  • 1
    What part of the error don't you understand? It's quite clear, in my view. Suppliers.Country isn't being aggregated, neither is it in the GROUP BY. Commented Apr 17, 2018 at 11:46
  • just include Suppliers.Country in group by clause. GROUP BY Categories.CategoryName, Suppliers.Country; Commented Apr 17, 2018 at 11:46
  • 1
    @iSR5 I wouldn't recommend that, I doubt it'll give the Op what they're after. Commented Apr 17, 2018 at 11:47
  • @Larnu yes, but it'll be a way to understand what was the issue and how to fix it. So, after that, he'll know what to do next ;) . . Commented Apr 17, 2018 at 11:49
  • @ISR5 It works, but the result is different, I've received 45 rows instead of 21 (number of categories) Commented Apr 17, 2018 at 11:50

1 Answer 1

1

Wrap your original query up in a derived table. Aggregate its result:

select "Categories", "Supplier Continent", sum("UnitsInStock") as "Units In Stock"
from
(
    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", 
            Products.UnitsInStock as "UnitsInStock" 
    FROM Suppliers
    INNER JOIN Products ON Products.SupplierID=Suppliers.SupplierID
    INNER JOIN Categories ON Categories.CategoryID= Products.CategoryID
) dt
GROUP BY "Categories", "Supplier Continent";
Sign up to request clarification or add additional context in comments.

1 Comment

Code is different, but result is correct. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.