Thursday 12 September 2013

Query to remove all redundant entries from a table

Query to remove all redundant entries from a table

pI have a Postgres table that describes relationships between entities,
this table is populated by a process which I cannot modify. This is an
example of that table:/p precode+-----+-----+ | e1 | e2 | |-----+-----| |
A | B | | C | D | | D | C | | ... | ... | +-----+-----+ /code/pre pI want
to write a SQL query that will remove all unecessary relationships from
the table, for example the relationship code[D, C]/code is redundant as
it's already defined by code[C, D]/code. /p pI have a query that deletes
using a self join but this removes everything to do with the relationship,
e.g.:/p precodeDELETE FROM foo USING foo b WHERE foo.e2 = b.e1 AND foo.e1
= b.e2; /code/pre pResults in:/p precode+-----+-----+ | e1 | e2 |
|-----+-----| | A | B | | ... | ... | +-----+-----+ /code/pre pHowever, I
need a query that will leave me with one of the relationships, it doesn't
matter which relationship remains, either code[C, D]/code or code[D,
C]/code but not both./p pI feel like there is a simple solution here but
it's escaping me./p

No comments:

Post a Comment