0

I have a table with ids and geometries. I am trying to return all ids which are intersect with another id.

My best query that I managed to arrive to:

SELECT a.id, b.id
FROM t1
WHERE st_equals(a.geom, b.geom) IS FALSE AND ST_intersects(a.geom, b.geom);

For example:

ID geometry
1 0103000020E6100000010000000500
2 1103000020E6100000010000000500
3 2103000020E6100000010000000500
4 3103000020E6100000010000000500
5 4103000020E6100000010000000500
6 5103000020E6100000010000000500
7 6103000020E6100000010000000500
8 7103000020E6100000010000000500

The desired result: {1,2,3,4}, {5,6,7,8}

Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Sep 22, 2022 at 10:39
1
  • 1
    You were almost there, but didn't define what "a" and "b" were (plus ST_Equals is a really expensive way to test identity (and technically wouldn't work, since it could return True on records other than a.id == b.id)). Next time, please include the error generated by your query. Commented Sep 22, 2022 at 11:57

1 Answer 1

1

Join the table to itself on intersects, where id's are not the same:

select a.id a_id, b.id b_id
from public.opbuff a
join
public.opbuff b
on st_intersects(a.geom, b.geom)
where a.id<b.id

enter image description here

answered Sep 22, 2022 at 11:11

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.