Error de Postgres "falta la entrada de la cláusula FROM" en la consulta con la cláusula WITH

I am trying to use this query in Postgres 9.1.3:

WITH stops AS (
    SELECT citation_id,
           rank() OVER (ORDER BY offense_timestamp,
                     defendant_dl,
                     offense_street_number,
                     offense_street_name) AS stop
    FROM   consistent.master
    WHERE  citing_jurisdiction=1
)

UPDATE consistent.master
SET arrest_id = stops.stop
WHERE citing_jurisdiction=1
  AND stops.citation_id = consistent.master.citation_id;

Me sale este error:

ERROR:  missing FROM-clause entry for table "stops"
LINE 12: SET arrest_id = stops.stop
                         ^

********** Error **********

ERROR: missing FROM-clause entry for table "stops"
SQL state: 42P01
Character: 280

I'm really confused. The WITH clause appears correct per Postgres documentation. If I separately run the query in the WITH clause, I get correct results.

preguntado el 10 de marzo de 12 a las 04:03

Whoops! Thanks. I was going to say I tried renamed the se detiene table as a diagnostic step, but that's clearly not the issue. -

2 Respuestas

Desde el fino manual:

There are two ways to modify a table using information contained in other tables in the database: using sub-selects, or specifying additional tables in the FROM cláusula.

So you just need a FROM clause:

WITH stops AS (
    -- ...
)
UPDATE consistent.master
SET arrest_id = stops.stop
FROM stops -- <----------------------------- You missed this
WHERE citing_jurisdiction=1
  AND stops.citation_id = consistent.master.citation_id;

The error message even says as much:

ERROR: missing FROM-clause entry for table "stops"

Respondido 27 Oct 15, 17:10

You're right! They need to give out "bonehead" points here. I think I just earned one. - aren cambre

@ArenCambre: I think we'd all have an excess of "bonehead" points if there were such a thing :) Some of the hardest problems to see are the ones right in front of your face. - mu es demasiado corto

Very nice answer. Just a small question: it should be FROM stops instead of FROM stop, right? - joragupra

@joragupra Yes, it should have been (and now is) stops, Gracias. - mu es demasiado corto

This can also happen if you mistype a table name. For example:

UPDATE profiles SET name = ( profile.first_name ) WHERE id = 1

En lugar de profiles i incorrectly used profile !! This would work:

UPDATE profiles SET name = ( profiles.first_name ) WHERE id = 1

respondido 12 nov., 17:15

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.