web-dev-qa-db-de.com

Fehlercode 1111. Ungültige Verwendung der Gruppenfunktion

Das funktioniert also:

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

Ich muss jedoch nur die city_population_percent-Werte abrufen, die größer als 30 sind. Daher versuche ich Folgendes:

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

Und dann bekomme ich:

Fehlercode 1111. Ungültige Verwendung der Gruppenfunktion

Das heißt, es schlägt fehl, wenn ich diese Bedingung in WHERE hinzufüge:

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

Sie müssen diese Bedingung also in die HAVING -Klausel verschieben

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

Sie verwenden Aggregatfunktionen in einer where-Klausel, was in SQL nicht möglich ist.

Verwenden Sie stattdessen die Klausel MIT :

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