De-duplicating many-to-many relationships in MySQL lookup table -
i've inherited database includes lookup table find other patents related given patent.
so looks
╔════╦═══════════╦════════════╗ ║ id ║ patent_id ║ related_id ║ ╠════╬═══════════╬════════════╣ ║ 1 ║ 1 ║ 2 ║ ║ 2 ║ 1 ║ 3 ║ ║ 3 ║ 2 ║ 1 ║ ║ 4 ║ 2 ║ 3 ║ ║ 5 ║ 3 ║ 2 ║ ╚════╩═══════════╩════════════╝
and want filter out reciprocal relationships. 1->2 , 2->1 same purposes want 1->2.
i don't need make edit in table, need query returns list of unique relationships, , while i'm sure it's simple i've been banging head against keyboard far long.
here clever query can try using. general strategy identify unwanted duplicate records , subtract them away entire set.
select t.id, t.patent_id, t.related_id t left join ( select t1.patent_id t1_patent_id, t1.related_id t1_related_id t t1 left join t t2 on t1.related_id = t2.patent_id t1.patent_id = t2.related_id , t1.patent_id > t1.related_id ) t3 on t.patent_id = t3.t1_patent_id , t.related_id = t3.t1_related_id t3.t1_patent_id null
here inner temporary table generated query. can convince applying logic in where
clause select correct records. non-duplicate records characterized t1.patent_id
!= t2.related_id
, , these records retained. in case of duplicates (t1.patent_id
= t2.related_id
), record chosen each pair of duplicates 1 patent_id
< related_id
, requested in question.
╔════╦══════════════╦═══════════════╦══════════════╦═══════════════╗ ║ id ║ t1.patent_id ║ t1.related_id ║ t2.patent_id ║ t2.related_id ║ ╠════╬══════════════╬═══════════════╬══════════════╬═══════════════╣ ║ 1 ║ 1 ║ 2 ║ 2 ║ 1 ║ * duplicate ║ 1 ║ 1 ║ 2 ║ 2 ║ 3 ║ ║ 2 ║ 1 ║ 3 ║ 3 ║ 2 ║ ║ 3 ║ 2 ║ 1 ║ 1 ║ 2 ║ * duplicate ║ 3 ║ 2 ║ 1 ║ 1 ║ 3 ║ ║ 4 ║ 2 ║ 3 ║ 3 ║ 2 ║ * duplicate ║ 5 ║ 3 ║ 2 ║ 2 ║ 1 ║ ║ 5 ║ 3 ║ 2 ║ 2 ║ 3 ║ * duplicate ╚════╩══════════════╩═══════════════╩══════════════╩═══════════════╝
click link below running example of query.
Comments
Post a Comment