web-dev-qa-db-fra.com

Code d'erreur 1111. Utilisation non valide de la fonction de groupe

Donc ça marche:

SELECT c.name AS country_name, c.population AS country_population, SUM(ci.population) AS city_population, ROUND(100*(SUM(ci.population)/c.population)) AS city_population_percent
FROM country AS c
JOIN city AS ci
ON c.code = ci.countrycode
WHERE c.continent = 'Europe'
GROUP BY c.name

Mais je dois seulement saisir les valeurs city_population_percent supérieures à 30, alors j'essaye ceci:

SELECT c.name AS country_name, c.population AS country_population, SUM(ci.population) AS city_population, ROUND(100*(SUM(ci.population)/c.population)) AS city_population_percent
FROM country AS c
JOIN city AS ci
ON c.code = ci.countrycode
WHERE c.continent = 'Europe'
**AND ROUND(100*(SUM(ci.population)/c.population)) > 30**
GROUP BY c.name

Et c'est là que je reçois:

Code d'erreur 1111. Utilisation non valide de la fonction de groupe

Autrement dit, il échoue lorsque j'ajoute cette condition dans le WHERE:

AND ROUND(100*(SUM(ci.population)/c.population)) > 30
16
user3374108

Vous devez donc déplacer cette condition vers la clause AYANT

SELECT c.name AS country_name, c.population AS country_population, SUM(ci.population) AS city_population, ROUND(100*(SUM(ci.population)/c.population)) AS city_population_percent
            FROM country AS c
            JOIN city AS ci
            ON c.code = ci.countrycode
WHERE c.continent = 'Europe'
GROUP BY c.name
HAVING ROUND(100*(SUM(ci.population)/c.population)) > 30
29
Surabhil Sergy

Vous utilisez des fonctions d'agrégation dans une clause where, ce que vous ne pouvez pas faire en SQL.

Utilisez à la place la clause AYANT :

WHERE c.continent = 'Europe'
GROUP BY c.name
HAVING ROUND(100*(SUM(ci.population)/c.population)) > 30
5
nos