I have a challenge with some sql that I don't know how to get my head around. That's why I want yours input to help me guide in the right direction to find a solution.
The goal of the procedure that I would like to create, is to fill the following table with valid combinations of values. The columns represent six positions in a string. Like a sentence with six words, where only a certain pattern is allowed.
+------+------+------+------+------+------+
| Pos1 | Pos2 | Pos3 | Pos4 | Pos5 | Pos6 |
+------+------+------+------+------+------+
| AB | CD | FS | KE | HA | KA |
+------+------+------+------+------+------+
| .. | .. | .. | .. | .. | .. |
+------+------+------+------+------+------+
A combination (pattern) of the six positions is valid when all positions of the combination are validated against many other tables. In total there are 30 tables containing valid combinations of two of the positions. There are two tables per combination of positions. For example: one containing valid combinations Pos1 To Pos2 (Left to Right) And one containing valid combinations Pos2 To Pos1 (Right to Left). Table examples:
TableA
+------+------+
| Pos1 | Pos2 |
+------+------+
| AB | CD |
+------+------+
| AB | EF |
+------+------+
| AA | GG |
+------+------+
TableB
+------+------+
| Pos1 | Pos2 |
+------+------+
| AB | YT |
+------+------+
| AA | YT |
+------+------+
| AB | CD |
+------+------+
TableC
+------+------+
| Pos2 | Pos3 |
+------+------+
| CD | FF |
+------+------+
| CD | UE |
+------+------+
| EF | KH |
+------+------+
TableD
+------+------+
| Pos2 | Pos3 |
+------+------+
| CD | FE |
+------+------+
| CD | UE |
+------+------+
| EF | KY |
+------+------+
TableE
+------+------+
| Pos1 | Pos6 |
+------+------+
| AB | ZZ |
+------+------+
| AB | ZF |
+------+------+
| AA | ZT |
+------+------+
On so on....
I know that I can start using INTERSECT statements to intersect all combinations between two tables with the same positions, for example
INSERT INTO TempTable12
( SELECT *
FROM TableA
INTERSECT
SELECT *
FROM TableB ) Valid12;
INSERT INTO TempTable23
( SELECT *
FROM TableC
INTERSECT
SELECT *
FROM TableD ) Valid23;
And so on...
..but how can I do that when I want to continue with more than two positions? In other words to combine different positions in the query. Should I use temp tables like in to example above? Are there better ways?
I hope this is not too cryptic for you to understand and still help me get started. Thanks in advance!