Skip to content
Advertisement

group by two joined columns

I get an error from this code:

Msg 8120 Level 16.
Column ‘T2.a’ is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

I would have expected this to work because T1.a and T2.a are joined

Advertisement

Answer

I would have expected this to work because T1.a and T2.a are joined

This makes sense, but unfortunately: no. SQL Server sees two different unaggregated columns in the SELECT clause, and wants them in the GROUP BY clause too:

On the other hand, since you are joining on these columns, you also know that both have the same value, so why would you need to output both in the resultset?

Some databases support the USING syntax on joins, which disambiguate the column names when both are the same – but not SQL Server:

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement