TransWikia.com

ERROR: more than one row returned by a subquery used as an expressionSQL state: 21000 Why and how it happens?

Geographic Information Systems Asked on March 17, 2021

I want to update an empty column with a join :

UPDATE icpe_modifie
SET region =
(SELECT nom_reg_m FROM icpe_modifie as icpe 
JOIN regions as reg 
ON ST_WITHIN(icpe.geom, reg.geom))

icpe_modifie is a point table and regions is a polygonal table.
I want to add the name of the region where is located the point.
But it returns :

ERROR:  more than one row returned by a subquery used as an expressionSQL state: 21000

So I tried :

SELECT nom_reg_m FROM icpe_modifie as icpe 
JOIN regions as reg 
ON ST_WITHIN(icpe.geom, reg.geom)

This second query is well executed and no error is raised.

The current solution I chose is to LIMIT 1 :

UPDATE icpe_modifie
SET region =
(SELECT nom_reg_m FROM icpe_modifie as icpe 
JOIN regions as reg 
ON ST_WITHIN(icpe.geom, reg.geom) LIMIT 1)

But I would like to better understand the error statement, to it means that join returns for at least one row several items.
I do not understand how a point could be in two different areas. And I also would like to check the points for which this is happening and have no idea to find these trouble points.

One Answer

I made a mistake while building my query. I am running an UPDATE and a JOIN.

The proper way to do is :

UPDATE t1
SET t1.c1 = new_value
FROM t2
WHERE t1.c2 = t2.c2;

See more here.

I was trying to fit the whole result in each single row.

Answered by Basile on March 17, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP