I would like to know what data structure / storage strategy I should use for this problem.
Each data entry in the database consists of a list of multiple ordered items, such as A-B-C-D, where A, B, C, D are different items.
Suppose I have 3 entries in a database,
A-B-C-D
E-F-G
G-H-B-A
When the user entered some unordered items, I have to find the matching ordered entry(ies) from the database. For example, if user enters A,B,G,H, I want to return G-H-B-A from the database to the user.
What should be my data storage strategy?
You’re best off storing the ordered and unordered elements separately, otherwise you’ll need to search on all permutations of the ordered elements, which would be time consuming.
Try this:
The above will return 3:GHBA, like you want. If you pass in DCBA you’ll get back 1:ABCD, again like you’re looking for. If you pass in C, you’ll get back nothing, as no group consists of just C.
You will probably want to use a table-valued parameter for your input, as shown above, but you could convert the final SELECT to a simple list and drop the ItemList type.