×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Finding records that have no values in common

Finding records that have no values in common

Finding records that have no values in common

(OP)
I have a table, below, where each record has 3 distinct values between 1 and 9.

CODE

ID    v1    v2    v3
---------------------
 1     1     6     2
 2     4     8     1
 3     3     5     4
 4     3     8     5
 5     8     7     9
 6     etc... 


I want to identify all sets of three IDs that, collectively, hold ALL values from 1 to 9.
So, each of the three IDs must have NO VALUES IN COMMON.

For example, in the table above, ID values 1, 3, and 5 hold all numbers 1 to 9.

My current sql solves the problem correctly, but it is unwieldy. (especially if I scale the problem up to, say,
5 IDs that show ALL numbers from 1 to 15).

Here's my current sql...

CODE

SELECT 
	t1.ID AS ID1, t2.ID AS ID2, t3.ID AS ID3
FROM 
	Tbl AS t1, Tbl AS t2, Tbl AS t3
WHERE 
	(t1.v1 <> t2.v1) AND (t1.v1 <> t2.v2) AND (t1.v1 <> t2.v3) AND
	(t1.v2 <> t2.v1) AND (t1.v2 <> t2.v2) AND (t1.v2 <> t2.v3) AND 
	(t1.v3 <> t2.v1) AND (t1.v3 <> t2.v2) AND (t1.v3 <> t2.v3) AND 

	(t1.v1 <> t3.v1) AND (t1.v1 <> t3.v2) AND (t1.v1 <> t3.v3) AND
	(t1.v2 <> t3.v1) AND (t1.v2 <> t3.v2) AND (t1.v2 <> t3.v3) AND 
	(t1.v3 <> t3.v1) AND (t1.v3 <> t3.v2) AND (t1.v3 <> t3.v3) AND 

	(t2.v1 <> t3.v1) AND (t2.v1 <> t3.v2) AND (t2.v1 <> t3.v3) AND
	(t2.v2 <> t3.v1) AND (t2.v2 <> t3.v2) AND (t2.v2 <> t3.v3) AND 
	(t2.v3 <> t3.v1) AND (t2.v3 <> t3.v2) AND (t2.v3 <> t3.v3) AND

        (t1.ID < t2.ID) AND (t1.ID < t3.ID) AND (t2.ID < t3.ID)
ORDER BY 
	t1.ID; 

Is there a more efficient way to do this? Thank you in advance for any hints.

Teach314

RE: Finding records that have no values in common

After normalizing your table (Get&Transform in excel, in access you can do it with union of a set of queries if it is your original structure) to Tbl2:
ID	Col	V
1	v1	1
1	v2	6
1	v3	2
2	v1	4
2	v2	8
2	v3	1
3	v1	3
3	v2	5
3	v3	4
4	v1	3
4	v2	8
4	v3	5
5	v1	8
5	v2	7
5	v3	9 
The query:
SELECT T_1.ID AS T1_ID, T_2.ID AS T2_ID, T_3.ID AS T3_ID, Count(T_1.ID) AS N
FROM Tbl2 AS T_1, Tbl2 AS T_2, Tbl2 AS T_3
WHERE (((([T_1].[V]=[T_2].[V] Or [T_1].[V]=[T_3].[V] Or [T_2].[V]=[T_3].[V]))=False))
GROUP BY T_1.ID, T_2.ID, T_3.ID
HAVING (((T_2.ID)>[T_1].[ID]) AND ((T_3.ID)>[T_2].[ID]) AND ((Count(T_1.ID))=27));

returns IDs
Scaling:
[Tst] field has all 2-elements combinations, here 3 conditions. For k=5 elements for each ID there are 10 test conditions. I would create a simple VBA function instead,with paramarray input for testing, in this case max k-1 tests in chain.

EDIT:
Of course the number in last sentence is not true, max number of tests is k(k-1)/2.


combo

RE: Finding records that have no values in common

(OP)
Excellent, combo. Very instructive!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close