I have an interesting problem. I have to assign an ID to a group of orders, based on whether they are packed in the same group of containers or not. One order may be in one or many containers, which means that not all containers in the group contain all the orders. For example, given these orders:
ORDER1 is in container A and B
ORDER2 is in container B and C
ORDER3 is in container C and D
ORDER4 is in container E
There should be two groups, the first containing ORDER1, ORDER2 and ORDER3, and the second containing only ORDER4. Notice that ORDER1 and ORDER3 do not share any containers.
I can think of a reasonably straightforward procedural algorithm for doing this grouping – getting the details right might be a bit painful though.
However, I like to have an SQL based solution if possible, but it’s beyond my grasp. I am using Oracle 10.2 – I am guessing that some funky features might come in to play here.
This is an interesting question, similar to this SO. You can build a query following the same approach:
Update
I tried to generate some more data to reproduce your performance problem. With only a thousand orders the query indeed doesn’t return in a timely fashion.
I tried to tweak the query with the CONNECT BY and START WITH clause but didn’t manage to improve performance. My next idea was to display the data in a more traditional hierarchical view:
This in turn is the base for the following query that did quite well on my test data set:
I used the following query to populate my data set (1200 rows):