Using the following data:
CREATE TABLE a (
x int
);
CREATE TABLE b (
y int
);
INSERT INTO a VALUES
(1), (2);
INSERT INTO b VALUES
(1), (1), (2), (2) ;
I'm trying to query as:
WITH a_cte AS (
SELECT min(x) AS min_a
FROM a
)
SELECT * FROM b
WHERE b.y <= a_cte.min_a;
But am receiving the error:
SQL Error [42P01]: ERROR: missing FROM-clause entry for table "a_cte"
I don't understand the reasoning here, as I thought I'd created the temp table a_cte
and could freely use it within the scope.
Note - this is on PostgreSQL 14.2
WHERE b.y <= (SELECT MIN(x) FROM a)
is perfectly valid unless you're getting paid by the line.