SQL Server SQL query with COUNT and INNER JOIN

0

I have a page in PHP where I have to return data from two tables that are in a SQL Server database.

One of the banks is from registered sellers and the other is from the categories where they are registered. I need to make a comparison and check how many sellers are registered by category.

I made the following query:

SELECT Vendedor.IdCanal, Count(Vendedor.IdCanal) total, CanalVenda.Nome FROM Vendedor INNER JOIN CanalVenda On Vendedor.IdCanal=CanalVenda.Id GROUP BY Vendedor.IdCanal

The problem is that this query does not return the field where the name of the Category. When I add it in the search, it returns the following error:

[Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Column 'CanalVenda.Nome' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

How to properly return results from field Nome ?

    
asked by anonymous 14.04.2016 / 20:43

1 answer

2

You have to group by that field as well.

SELECT Vendedor.IdCanal, Count(Vendedor.IdCanal) total, 
CanalVenda.Nome FROM      Vendedor INNER JOIN CanalVenda 
On Vendedor.IdCanal=CanalVenda.Id GROUP BY Vendedor.IdCanal, 
CanalVenda.Nome
    
14.04.2016 / 20:53