Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3631516
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:24:26+00:00 2026-05-19T00:24:26+00:00

I have an interesting problem. I have to assign an ID to a group

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T00:24:27+00:00Added an answer on May 19, 2026 at 12:24 am

    This is an interesting question, similar to this SO. You can build a query following the same approach:

    SQL> WITH orders AS (
      2     SELECT 'ORDER1' ord, 'A' cont FROM dual
      3     UNION ALL SELECT 'ORDER1' , 'B' FROM dual
      4     UNION ALL SELECT 'ORDER2' , 'B' FROM dual
      5     UNION ALL SELECT 'ORDER2' , 'C' FROM dual
      6     UNION ALL SELECT 'ORDER3' , 'C' FROM dual
      7     UNION ALL SELECT 'ORDER3' , 'D' FROM dual
      8     UNION ALL SELECT 'ORDER4' , 'E' FROM dual
      9  )
     10  SELECT ord, MIN(grp) "group" /*, cont*/
     11    FROM (SELECT connect_by_root(ord) ord,
     12                 connect_by_root(cont) cont,
     13                 cont grp
     14             FROM orders
     15           CONNECT BY NOCYCLE(cont = PRIOR cont
     16                           OR ord = PRIOR ord))
     17  GROUP BY ord /*, cont*/
     18  ORDER BY ord, MIN(grp);
    
    ORD    group
    ------ -----
    ORDER1 A
    ORDER2 A
    ORDER3 A
    ORDER4 E
    

    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:

    SQL> SELECT o1.ord "order", o2.ord "is connected to"
      2    FROM orders o1
      3    JOIN orders o2 ON o1.cont = o2.cont
      4                  AND o1.ord < o2.ord;
    
    order  is connected to
    ------ ---------------
    ORDER1 ORDER2
    ORDER2 ORDER3
    

    This in turn is the base for the following query that did quite well on my test data set:

    SQL> SELECT o.ord, nvl(MIN(connexions.grp), o.ord) grp
      2    FROM orders o
      3    LEFT JOIN (SELECT connect_by_root(ord1) grp, ord2
      4                      --, sys_connect_by_path(ord1, '->')
      5                 FROM (SELECT o1.ord ord1, o2.ord ord2
      6                          FROM orders o1
      7                          JOIN orders o2 ON o1.cont = o2.cont
      8                                        AND o1.ord < o2.ord)
      9               CONNECT BY PRIOR ord2 = ord1
     10                ORDER BY 1, 2) connexions ON o.ord = connexions.ord2
     11    GROUP BY o.ord
     12    order by 1,2;
    
    ORD    GRP
    ------ ------
    ORDER1 ORDER1
    ORDER2 ORDER1
    ORDER3 ORDER1
    ORDER4 ORDER4
    

    I used the following query to populate my data set (1200 rows):

    CREATE TABLE orders AS
    SELECT 'ORDER' || to_char(dbms_random.VALUE(0, 1000), 'fm000000') ord,
           to_char(dbms_random.VALUE(0, 800), 'fm000000') cont
      FROM dual
    CONNECT BY LEVEL <= 1200;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an interesting problem with the TempData object not passing values to another
I have one interesting problem. I must parse mail body (regular expression), get some
We have an interesting problem with WCF binding and streaming transfer mode that we
I have some interesting problem for an hour.. In my flex project, all width
I have an interesting problem. The basis of the problem is that my last
I have an interesting problem when using partial page update in asp.net with scriptmanager
I have an interesting problem, which is a function that returns a Dictionary<String,HashSet<String>> .
I have an interesting problem. I wrote the following perl script to recursively loop
I have an interesting problem here I've been trying to solve for the last
I have an interesting problem being reported to me from an android application I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.