SQL Recipes (Beta II)
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 Rate Thread
  #1 (permalink)  
Old 02-07-2007, 10: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!
Edit/Delete Message Reply With Quote

ANSWER(S):

  #2 (permalink)  
Old 02-08-2007, 06: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!
Edit/Delete Message Reply With Quote
  #3 (permalink)  
Old 02-08-2007, 11:07 PM
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!
Edit/Delete Message Reply With Quote
  #4 (permalink)  
Old 02-09-2007, 05:36 AM
Dimitar
 
Posts: n/a
Unhappy ANY answer. Re: Find matches with no repeats?

U need antivirus?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may 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

Moderation Tools:



All times are GMT. The time now is 09:27 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-2007 SQL Recipes

1 2 3 4 5 6 7