Remove duplicate comparisons in postgres?
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
1
down vote
favorite
I have a polygon layer named scale_polygons_v4
. I want to get ids of overlapping polygons. I have tried the code below:
select distinct s1.id,s2.id from scale_polygons_v4 s1
inner join scale_polygons_v4 s2
on s1.id!=s2.id and s1.kind=s2.kind and st_overlaps(s1.geom,s2.geom)
The results are correct, but with duplicate comparisons it returns for example:
10029161,10011031
10011031,10029161
There are multiple such cases I only want to get one comparison.
postgresql distinct
New contributor
user3052682 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
1
down vote
favorite
I have a polygon layer named scale_polygons_v4
. I want to get ids of overlapping polygons. I have tried the code below:
select distinct s1.id,s2.id from scale_polygons_v4 s1
inner join scale_polygons_v4 s2
on s1.id!=s2.id and s1.kind=s2.kind and st_overlaps(s1.geom,s2.geom)
The results are correct, but with duplicate comparisons it returns for example:
10029161,10011031
10011031,10029161
There are multiple such cases I only want to get one comparison.
postgresql distinct
New contributor
user3052682 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a polygon layer named scale_polygons_v4
. I want to get ids of overlapping polygons. I have tried the code below:
select distinct s1.id,s2.id from scale_polygons_v4 s1
inner join scale_polygons_v4 s2
on s1.id!=s2.id and s1.kind=s2.kind and st_overlaps(s1.geom,s2.geom)
The results are correct, but with duplicate comparisons it returns for example:
10029161,10011031
10011031,10029161
There are multiple such cases I only want to get one comparison.
postgresql distinct
New contributor
user3052682 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I have a polygon layer named scale_polygons_v4
. I want to get ids of overlapping polygons. I have tried the code below:
select distinct s1.id,s2.id from scale_polygons_v4 s1
inner join scale_polygons_v4 s2
on s1.id!=s2.id and s1.kind=s2.kind and st_overlaps(s1.geom,s2.geom)
The results are correct, but with duplicate comparisons it returns for example:
10029161,10011031
10011031,10029161
There are multiple such cases I only want to get one comparison.
postgresql distinct
postgresql distinct
New contributor
user3052682 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
user3052682 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 22 mins ago
Colin 't Hart
6,30582332
6,30582332
New contributor
user3052682 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 1 hour ago
user3052682
1061
1061
New contributor
user3052682 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
user3052682 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
user3052682 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
You can use least()
and greatest()
for this:
select distinct least(s1.id, s2.id), greatest(s1.id, s2.id)
from scale_polygons_v4 s1
join scale_polygons_v4 s2
on s1.id <> s2.id
and s1.kind = s2.kind
and st_overlaps(s1.geom,s2.geom);
Another option is to do this in the join condition:
select distinct s1.id, s2.id
from scale_polygons_v4 s1
join scale_polygons_v4 s2
on s1.id > s2.id
and s1.kind = s2.kind
and st_overlaps(s1.geom,s2.geom);
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
You can use least()
and greatest()
for this:
select distinct least(s1.id, s2.id), greatest(s1.id, s2.id)
from scale_polygons_v4 s1
join scale_polygons_v4 s2
on s1.id <> s2.id
and s1.kind = s2.kind
and st_overlaps(s1.geom,s2.geom);
Another option is to do this in the join condition:
select distinct s1.id, s2.id
from scale_polygons_v4 s1
join scale_polygons_v4 s2
on s1.id > s2.id
and s1.kind = s2.kind
and st_overlaps(s1.geom,s2.geom);
add a comment |Â
up vote
2
down vote
You can use least()
and greatest()
for this:
select distinct least(s1.id, s2.id), greatest(s1.id, s2.id)
from scale_polygons_v4 s1
join scale_polygons_v4 s2
on s1.id <> s2.id
and s1.kind = s2.kind
and st_overlaps(s1.geom,s2.geom);
Another option is to do this in the join condition:
select distinct s1.id, s2.id
from scale_polygons_v4 s1
join scale_polygons_v4 s2
on s1.id > s2.id
and s1.kind = s2.kind
and st_overlaps(s1.geom,s2.geom);
add a comment |Â
up vote
2
down vote
up vote
2
down vote
You can use least()
and greatest()
for this:
select distinct least(s1.id, s2.id), greatest(s1.id, s2.id)
from scale_polygons_v4 s1
join scale_polygons_v4 s2
on s1.id <> s2.id
and s1.kind = s2.kind
and st_overlaps(s1.geom,s2.geom);
Another option is to do this in the join condition:
select distinct s1.id, s2.id
from scale_polygons_v4 s1
join scale_polygons_v4 s2
on s1.id > s2.id
and s1.kind = s2.kind
and st_overlaps(s1.geom,s2.geom);
You can use least()
and greatest()
for this:
select distinct least(s1.id, s2.id), greatest(s1.id, s2.id)
from scale_polygons_v4 s1
join scale_polygons_v4 s2
on s1.id <> s2.id
and s1.kind = s2.kind
and st_overlaps(s1.geom,s2.geom);
Another option is to do this in the join condition:
select distinct s1.id, s2.id
from scale_polygons_v4 s1
join scale_polygons_v4 s2
on s1.id > s2.id
and s1.kind = s2.kind
and st_overlaps(s1.geom,s2.geom);
answered 38 mins ago
a_horse_with_no_name
36.9k771109
36.9k771109
add a comment |Â
add a comment |Â
user3052682 is a new contributor. Be nice, and check out our Code of Conduct.
user3052682 is a new contributor. Be nice, and check out our Code of Conduct.
user3052682 is a new contributor. Be nice, and check out our Code of Conduct.
user3052682 is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f220092%2fremove-duplicate-comparisons-in-postgres%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password