I am trying to create a query in SQL Server that will search for all combinations of numbers in a table.
COMBINATION TABLE
CombID Comb_Num1 Comb_NumTwo Comb_NumThree
1 1 2 3
2 2 10 15
3 5 20 60
4 10 22 50
5 22 33 46
The numbers range from 1-60, and the same number is not repeated within a combination. Order does not matter.
ENTRY TABLE
EntryID NumberOne NumberTwo NumberThree NumberFour NumberFive
1 10 22 33 46 50
2 2 10 15 22 40
3 24 33 40 45 50
4 5 10 22 40 60
5 2 6 10 22 40
6 2 10 22 50 60
7 10 22 33 46 50
The numbers range from 1-60, and the same number is not repeated within an entry. Order does not matter.
Results
- Searching for combination 1 would produce no results
- Searching for combination 2 would return EntryID 2
- Searching for combination 3 would produce no results
- Searching for combination 4 would return EntryID 1, 6, 7
- Searching for combination 5 would return EntryID 1, 7
The query should also show for each record in the Combination table how many times it has appeared in the Entry table. It should exclude the combinations that don't appear in the Entry table.