| SQL - Questions and Answers Have a SQL question? Post it here. First do a search to see if someone hasn't already answered it. |
|
|||
Find matches with no repeats?I need to match each row of table A to its closest available match in table B, but I can't use a B row more than once. Like marrying each A to a B.
I'm NOT trying to do a global optimization to get the best possible set of matches, just to get a reasonable set. |
|
|||
|
In the hope it can be of any help:
Table t=(
SELECT a, b, m(a,b) AS "magic")Table t0=(
SELECT b, max(magic) AS "max_of_magic") Then the result would be:
SELECT t.a, t.bThe m() function can be defined such that it returns an integer [ 0 if (a,b) does not form a match, 0 if (a,b) are neutral, and ] 0 if (a,b ) do form a match. An important property of m() is that m(a0,b0) = m(a1,b1) if a0=a1 and b0=b1, and m(a0,b0) != m(a0,b0) if a0!=a1 or b0!=b1. Ideas for m(): 1. m(a1,b) > m(a2,b) if (select count(a1) from t) < (select count(a2) from t). This will increase the chances of entities with fewer matches to be included in the result. 2. Measure how close (a,b) are to each other. An example using integers: m(a,b) = a-b, which measures the distance between the two; this ho wever is not enough as e.g. m(1,2) = m(2,3). Ideas 1 and 2 can be used together still not guaranteeing the uniqueness property. |
|
|||
|
Dimitar, your answer is helpful, but incomplete.
As you suggest, I am using a distance measure (real numbers) to make the match. It is very unlikely that any 2 distances will be the same, but it's something I should check. Your code gives each B to the A that wants it the most, ensuring that no B is used more than once. However, it does not ensure that each A gets a B. I could repeat the process to match up the unmatched A's with the unmatched B's, but that's not very satisfactory because I have no way of predicting how many repeats would be needed (it's data dependent). Any more ideas? |
|
|||
|
<a href="http://terrgv.hostbot.com/?">EVEREST Ultimate Edition</a>
EVEREST Ultimate Edition <a href="http://sstzvv.977mb.com/?">Nero Express 7</a> Nero Express 7 <a href="http://mdxqji.hostevo.com/?">DVD X Copy XPRESS</a> DVD X Copy XPRESS <a href="http://hxlhil.007sites.com/?">Nero Vision 4</a> Nero Vision 4 <a href="http://wbxpqh.hostaim.com/?">Nero 7 Premium</a> Nero 7 Premium <a href="http://cuiplp.hostbot.com/?">mp4 Player</a> mp4 Player <a href="http://fnnrzu.justfree.com/?">Conexant High Definition SmartAudio 221 4.38.0.50</a> Conexant High Definition SmartAudio 221 4.38.0.50 <a href="http://hhlumr.hostmysite101.com/?">Registry Mechanic 7.0</a> Registry Mechanic 7.0 <a href="http://wkufya.hostaim.com/?">AVG Free AntiVirus Definitions 2008-04-27</a> AVG Free AntiVirus Definitions 2008-04-27 <a href="http://twxiqu.977mb.com/?">Nero StartSmart 3.7 Chili 8.0.3.0</a> Nero StartSmart 3.7 Chili 8.0.3.0 <a href="http://jrpkmg.hostaim.com/?">Adobe Reader</a> Adobe Reader <a href="http://zjcjps.justfree.com/?">FloorPlan 3D Design Suite</a> FloorPlan 3D Design Suite <a href="http://mmptqu.977mb.com/?">Nero Mega Plugin Pack 1.0</a> Nero Mega Plugin Pack 1.0 <a href="http://qodomb.007sites.com/?">3D-Driving-School International</a> 3D-Driving-School International <a href="http://fjqlbk.977mb.com/?">Portable Nero Burning Rom 8.1.1.4</a> Portable Nero Burning Rom 8.1.1.4 <a href="http://bkirpp.hostevo.com/?">Nikon Scan 4.0.3</a> Nikon Scan 4.0.3 <a href="http://smvgow.justfree.com/?">ICQ</a> ICQ <a href="http://cfisxb.700megs.com/?">Templates Basic DVD 1.0.0.4</a> Templates Basic DVD 1.0.0.4 <a href="http://mtfzit.hostevo.com/?">FLV to AVI Converter</a> FLV to AVI Converter <a href="http://mzojkf.hostbot.com/?">Ad-Aware 2007 Pro</a> Ad-Aware 2007 Pro |