SELECT
a.Name, a.About,
COUNT(b.Id) AS TotalCities,
SUM(b.NoOfDwellers) AS TotalCityDwellers
FROM
Countries a
LEFT JOIN
Cities b ON a.Id = b.CountryId
WHERE
a.Name LIKE '%some str%'
GROUP BY
a.Id
ORDER BY
a.Name ASC
This SQL returns error:
Msg 8120, Level 16, State 1, Line 1
Column 'Countries.Name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
SELECT
which has aGROUP BY
clause, every column in theSELECT
list must be either an aggregate (SUM
,COUNT
,MAX
etc.), or then it has to be in theGROUP BY
clause. You have botha.Name
anda.About
in yourSELECT
list which are not handled by an aggregate - therefore, these two columns must appear in theGROUP BY
clause – marc_sCountries.Name
is not contained in theGROUP BY
clause. – TT.a.Name
withMAX(a.Name)
in theSELECT
clause. – Damien_The_Unbeliever