Let me explain: When I make a GROUP BY
with MySQL, I can select fields that can not be grouped, as descriptions (varchars), and the result shows, of all the grouped values, the first one that I find.
But now I find SQL Server and it turns out that the same group by
fails, depending on the reason, because it "does not know" what to do with those fields if a grouping function has not been applied to them, such as SUM, AVG, COUNT ...
An example:
I have the following table:
ID GRUPO VALOR DESCRIPCION
--------------------------------
1 1 10 Prueba1
2 1 20 Prueba2
3 2 10 Prueba3
4 2 20 Prueba4
And I execute the following select:
SELECT GRUPO, AVG(VALOR), DESCRIPCION FROM 'GroupBy' group by GRUPO;
With this select, in MySQL I get:
GRUPO AVG(VALOR) DESCRIPCION
-------------------------------
1 15.0 Prueba1
2 15.0 Prueba3
But in SQL Server I get:
SQL Error 8120: La columna "DESCRIPCION" de la lista de selección no es válida, porque no está contenida en una función de agregado ni en la cláusula GROUP BY.
Is there a way to make a GROUP BY
in SQL Server that returns the same result that it would give me in MySQL?