SQL Recipes
A FREE cookbook for SQL queries and examples
Register FAQ Search Today's Posts Mark Forums Read

SQL - Questions and Answers Have a SQL question? Post it here. First do a search to see if someone hasn't already answered it.

Go Back   SQL Recipes a FREE cookbook of SQL queries and examples > SQL queries and examples > SQL - Questions and Answers

Reply
 
LinkBack Thread Tools Search this Thread
  #1 (permalink)  
Old 02-07-2007, 11:23 AM
Robert Potter
 
Posts: n/a
Default SQL 92 dialect question:

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

ANSWER(S):

  #2 (permalink)  
Old 02-08-2007, 07:40 AM
Dimitar
 
Posts: n/a
Default ANY answer. Re: Find matches with no repeats?

In the hope it can be of any help:
Table t=(
SELECT a, b, m(a,b) AS "magic"
FROM A,B
WHERE m(a,b) > 0
)

Table t0=(
SELECT b, max(magic) AS "max_of_magic"
FROM t
GROUP BY b

)

Then the result would be:
SELECT t.a, t.b
FROM t, t0
WHERE t.b = t0.b
    AND t.magic = t0.max_of_magic


The 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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-09-2007, 12:07 AM
Robert Potter
 
Posts: n/a
Default ANY answer. Re: Find matches with no repeats?

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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-09-2007, 06:36 AM
Dimitar
 
Posts: n/a
Unhappy ANY answer. Re: Find matches with no repeats?

<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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 08:05 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0
Copyright (c) 2006-2008 SQL Recipes

1 2 3 4 5 6 7 8