Reputation: 25
Hi everyone I was wondering if anyone could help me understand what I'm doing wrong, this query only returns one row when there are 100 rows in the table:
SELECT country, population, CASE
WHEN population > AVG(population) THEN "Above average Population"
ELSE "Below average Population"
END as "population_avg_status"
FROM countries_by_population;
I noticed that if I change AVG(population)
with the actual value of AVG(population)
I get 100 rows so I'm guessing the problem is the AVG
function.
Upvotes: 0
Views: 208
Reputation: 1270443
Use window functions:
SELECT country, population,
(CASE WHEN population > AVG(population) OVER ()
THEN 'Above average Population'
ELSE 'Below average Population'
END) as population_avg_status
FROM countries_by_population;
Some words about quotes:
'
), not the double quote ("
). Although some databases extend the functionality of double quotes to delimit strings, it is better to stick with the standard.Upvotes: 0
Reputation: 147206
AVG
is an aggregation function, and when an aggregation function is used without a GROUP BY
clause it causes the result to be aggregated over all rows. That is why you are only getting one row in your output. To work around this, compute the average in a subquery:
SELECT country,
population,
CASE WHEN population > (SELECT AVG(population) FROM countries_by_population) THEN "Above average Population"
ELSE "Below average Population"
END as population_avg_status
FROM countries_by_population;
Upvotes: 1