We've had an accident where multiple rows with duplicate values have been inserted into a table, and I need to find which rows in a rather specific format. So far, I have this query:
SELECT p2.id
FROM assignmentobject p1, assignmentobject p2
WHERE ST_Equals(p1.the_geom, p2.the_geom) AND
p1.id <> p2.id and p1.assignmentid = 15548
group by p1.id, p2.id
which compares the geometries of the rows and spits it out if it's the same. The IDs are primary keys, and are sequentially created.
However, this presents a problem, as this small segment of the result shows:
p1.id p2.id
35311 35314
35311 35315
35314 35311
35314 35315
35315 35311
35315 35314
As can be seen here, 35311, 35314, and 35315 have the same geometries, and because of this, all of the combinations between them are included in the result. What I'm aiming to achieve, is having the lowest or highest ID used as the "base", and ignore the other combinations that doesn't involve this "base". I.e., the result shown above would be:
p1.id p2.id
35311 35314
35311 35315
Here, the combinations between 31314 and 35315 are left out. Is this possible to achieve using pure SQL?