I have a table with items that are booked together and in a certain order. This results in a table with a data set like this.
id item_id group_id
1 1 1
2 2 1
3 3 1
4 1 2
5 2 2
6 3 2
7 2 3
8 1 3
9 3 3
10 3 4
11 2 4
12 1 4
13 1 5
14 2 5
15 3 5
16 4 5
.
.
.
Now, I am looking for a query (or multiple) that finds the different sort orders within the groups and that can indicate the dominant one. In this case the answer should be something like:
group_id order_used_nr_times
1 3
2 3
3 1
4 1
5 3
.
.
.
Note, as group 5 indicates, it is well possible that more items exist within the group and that the searched items are a subset (e.g.,looking for order of items 4,5,6 and found in 1,2,3,4,5,6,7,8,9 is an option.
I've been thinking about a query with group and having or something with mysql transpose but I can't get my head around it.
Additional info:
I need the query to give me the dominant sort order (this case 1,2,3
) so it can be used to insert a new group that consists of the items 1,2,3
ordered 1,2,3
and not 2,1,3
or 3,2,1,
in this example.
From a business perspective: There are two "groups of people" using the system, group A and Group B. Group A knows how to order the items, therefore sets the order manually and the systems just inserts the data in the given order. Group B however, doesn't know the order. Therefore the system (query) needs to look if Group A already booked these items and if so, in which order they occurs most often (order can differ as the example shows). The order from group A will then be used to insert the data from group B assuming this is the most logical.
I hope this explanation helps.