SQL Server query SQL with COUNT and INNER JOIN

Asked

Viewed 4,399 times

0

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

One of the banks is registered sellers and the other is of 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 consultation:

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 happens that this query does not return me the field where has the name category. When I add it to the query, 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 correctly return field results Nome?

1 answer

3


You have to group through that field too.

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
  • Thanks @Ginaldo-Rigo, it worked perfectly. That’s right, I needed to put the CanalVenda.Nome at Group By. Thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.