I have a simple SQL Server 2008 database with two tables:
TableA: (PK)"ID" "Field"
and
TableB: (PK)"ID" (FK)"ID_TableA" "Field"
I want to highlight all the fields in TableA , as well as the number of corresponding rows in TableB for each row of TableA :
SELECT A.*, COUNT(B."ID") as "B number" FROM "TableA" A LEFT JOIN "TableB" B ON (A."ID" = B."ID_TableA") GROUP BY A."ID", A."Field"
This works well, but I have this problem: if TableA further modified (say, we need to add another Field2 column), I must update the SELECT above to include this field in GROUP BY . Otherwise, I get this error when performing the operation:
"The column" TableA.Field2 "is not valid in the selection list because it is not contained in either the aggregate function or the GROUP BY clause"
Is there a way to avoid this, so I can change my TableA without updating all statements like the one above?
source share