I have this simple query
SELECT c1.id AS id,
c1.title AS title,
(
SELECT b1.price
FROM bids b1
WHERE b1.product = c1.id
ORDER BY b1.price DESC
LIMIT 1
) AS highest_offer
FROM products c1
ORDER BY highest_offer
however If I want to add this query
WHERE highest_offer = '538.16'
I gets error :
error: column "highest_offer" does not exist
Please help me
I tried different things but nothing worked.
2
Answers
You could rewrite this a limit query:
If there could be two or more titles tied for the highest price, and you want to display all ties, then more generally we can use
RANK
here:The simplest way would be to use your query as a tabular subquery in a
from
clauseor in a CTE