I have a table, below, where each record has 3 distinct values between 1 and 9.
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...
Is there a more efficient way to do this? Thank you in advance for any hints.
Teach314
Code:
ID v1 v2 v3
---------------------
1 [b]1 6 2[/b]
2 4 8 1
3 [b]3 5 4[/b]
4 3 8 5
5 [b]8 7 9[/b]
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