PostgreSQL - Using result of subquery in the top of select statement
Posted on 2014-04-05
I have a query i'm working that uses constant "8136454" value from the same table which is the average population of that table.
What i would like to know is if i can pre-calculate the average dynamically in a subquery or any other way and then put it inside of the select-statement, with the condition of not using temporary tables or PL/PgSQL.
Query with constant value:
SELECT *,ABS((population - 8136454))AS distance FROM country ORDER BY distance ASC;
Funtion that i used to calculate constant value "8136454":
SELECT AVG(population) FROM country;
Also if anyone has any good links on how to implement subqueries in various ways in PostgreSQL that would be thankful, as i'm finding it hard wrap my head around this concept.