How to use postgres subquery result to filter overall results

UAhmadSoft

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

and getting results like this

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.

Stefanov.sm

The simplest way would be to use your query as a tabular subquery in a from clause

select * from 
(
  /* your query here */ 
) as t
WHERE t.highest_offer = '538.16';

or in a CTE

with t as 
(
  /* your query here */ 
)
select * from t  
WHERE highest_offer = '538.16';

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to re-use the result of a subquery in the main query, with Postgres

How to use the result of a subquery

How to filter results based on subquery?

How to use the results of a query to filter a table comparing one field to the result?

How can I use a subquery on my query results and then ORDER BY a calculated result for each row?

How to use left function in subquery using Postgres

Postgres: How to use value of field in subquery

How i can use result of subquery in this query?

Postgres wraps subquery results in parentheses

Oracle correlated subquery, filter results

SQL insert use subquery result in another subquery

How to Order By a Subquery result

How to use insert query where one of the value is a result of a subquery

Is it possible to use LIMIT with a subquery result?

Use AVG result in select subquery

How to use a subquery to filter a sqlalchemy query on a one to many relationship?

update table only if subquery returns the result postgres

Postgres Reference a subquery result in where clause

How to use checkbox input to filter results?

How to use filter result with list integer in IQueryable

In postgres, how to sort on, among others, a result value of a string_agg function, without selecting that field? Subquery not possible

How to return result of this recursive function / overall improve it?

How to default a value in subquery result?

How to loop MySql subquery result?

Iterate through SQL result set adding subquery results to result set

Java Rest how to make an overall get filter

How can I find records in a subquery result where an item has a result which is false and has no results which are true?

How to join subquery results to function results

How to use limit in subquery?