Group by with error in SQL Server

0

Good morning, I would like to know why my SQL query is not working, I'm using SQL Server 2008 and my GROUP BY is going wrong. I already checked and the table names are correct and are filled correctly.

Code:

SELECT categoria, nome_categoria
FROM tabela_teste.dbo.teste
WHERE vigencia = '2016-04'
GROUP BY categoria
ORDER BY nome_categoria;

Error:

  

is invalid in the select list because it is not contained in either an   aggregate function or the GROUP BY clause.

    
asked by anonymous 07.06.2016 / 14:45

1 answer

4

All select fields must be grouped or contained in the GROUP BY clause or grouped by SUM, MIN, MAX

 select categoria, nome_categoria from tabela_teste.dbo.teste 
 where vigencia =   '2016-04' group by categoria, nome_categoria 
 order by nome_categoria;

Below are some selects that are syntactically correct:

select cdfilial, cdlocal, dslocal  from minhatabela group by cdfilial, cdlocal, dslocal

select min( cdfilial ), cdlocal, dslocal  from minhatabela group by cdlocal, dslocal

select min( cdfilial ), max( cdlocal ), min( dslocal )  from minhatabela

select sum( cdfilial ), max( cdlocal ), min( dslocal ) from minhatabela

You will determine how you want to group your data and what type of grouping you want.

    
07.06.2016 / 14:55