All unique combinations of values in the same column of a group?

Hi friends

I have a table t with data as follows
CREATE TABLE T
(order_number number,
item_name varchar2(3))
 
INSERT INTO T VALUES (1,'A1');
INSERT INTO T VALUES (1,'A2');
INSERT INTO T VALUES (1,'B1');
INSERT INTO T VALUES (1,'B2');
INSERT INTO T VALUES (1,'C1');
INSERT INTO T VALUES (1,'C2');
INSERT INTO T VALUES (2,'A4');
INSERT INTO T VALUES (2,'B1');
INSERT INTO T VALUES (2,'A3');
INSERT INTO T VALUES (2,'B5');
INSERT INTO T VALUES (2,'C1');
INSERT INTO T VALUES (2,'C2');

sql> SELECT ITEM_NAME, ORDER_NUMBER FROM T
/


item name   order number
A1             1
A2             1
B1             1
B2             1
C1             1
C2             1
...
...
A4             2
B1             2
A3             2
B5             2
C1             2
C2             2

In my table, each 'item' can appear just once in each 'order number'. 
I want to join each item name to all other item names (without any repitition), within the same order number. 


My desired output is like below:

item name     item name   order number
A1            A2          1
A1            B1          1
A1            B2          1
A1            C1          1
A1            C2          1
A2            B1          1    
A2            B2          1
A2            C1          1
A2            C2          1
B1            B2          1    
B1            C1          1
B1            C2          1
B2            C1          1
B2            C2          1
C1            C2          1    

......
......
......
There are 15 combinations so the number 1
.. .nCr where n = 6 and r = 2 NCR Yes = 15.


Note: x A1 A2 is ignored because already, A1, A2 came in the first row of my desired output. similar cases for others combinations should be ignored as well.


I want to have the shortest possible of a query with more rapid execution to achieve this goal. Because in my real table 2000 there order numbers and each order number has 10 unique objects.

I look forward to the answer.

concerning
Hamza

Published by: Hamza on July 10, 2011 22:55

Published by: Hamza on July 10, 2011 22:58
with t as (
           select 'A1' item_name,1 order_number from dual union all
           select 'A2',1 from dual union all
           select 'B1',1 from dual union all
           select 'B2',1 from dual union all
           select 'C1',1 from dual union all
           select 'C2',1 from dual
          )
-- end of on-the-fly data sample
select  t1.item_name,
        t2.item_name,
        t1.order_number
  from  t t1,
        t t2
  where t1.order_number = t2.order_number
    and t1.item_name < t2.item_name
  order by t1.order_number,
           t1.item_name,
           t2.item_name
/

ITEM_NAME  ITEM_NAME  ORDER_NUMBER
---------- ---------- ------------
A1         A2                    1
A1         B1                    1
A1         B2                    1
A1         C1                    1
A1         C2                    1
A2         B1                    1
A2         B2                    1
A2         C1                    1
A2         C2                    1
B1         B2                    1
B1         C1                    1

ITEM_NAME  ITEM_NAME  ORDER_NUMBER
---------- ---------- ------------
B1         C2                    1
B2         C1                    1
B2         C2                    1
C1         C2                    1

15 rows selected.

SQL> 

SY.
P.S. It your order_number big enough index table, nom_element would be useful.

Tags: Database

Similar Questions

  • Multiple values for the same column in the columns of diffétent in the same row?

    Hi all
    I wonder how you can display different values for the same column in different columns on the same line. For example, using a CASE statement, I can:

    CASE WHEN CODE IN ('1 ', ' 3') THEN COUNT (ID) AS 'Y '.
    CASE WHEN CODE NOT IN ('1 ', am') THEN COUNT (ID) AS "N".

    Yes, that will produce two columns but will produce null values to empty and also two separate registers.


    Any ideas?

    Thank you

    Are you sure that this code works for you?

    Select ID
             ,CASE WHEN MODE_CODE IN ('1', '3') THEN COUNT( No) END as "Fulltime"
             ,CASE WHEN MODE_CODE NOT IN ('1', '3') THEN COUNT( No ) END  as "Other"
    From table
    group by ID
    

    I guess the code above fails because MODE_CODE is not in your group by?

    My suggestion would be to put the CASE in the COUNT:

    Select ID
             ,COUNT(CASE WHEN MODE_CODE IN ('1', '3') THEN No END) as "Fulltime"
             ,COUNT(CASE WHEN MODE_CODE NOT IN ('1', '3') THEN No END)  as "Other"
    From table
    group by ID
    

    CASE expressions return no. when the respective conditions are true and NULL otherwise.
    COUNTY will have non-null values.

  • Select literal values in the same column

    Hello
    like the practice:

    Select 'A', 'B', 'C' for double
    will select these values in their own column on the same line, is there a simple way to select these values in the same column on lines without first their insertion into a table?

    Thank you
    SQL> select 'A' x from dual union
      2  select 'B' x from dual union
      3  select 'C' x from dual ;
    
    X
    -
    A
    B
    C
    

    A bit complicated:

    SQL> select chr(ascii('A')+level-1) x
      2  from dual
      3  connect by level <=3;
    
    X
    -
    A
    B
    C
    

    Other:

    SQL> select regexp_substr('ABC','[[:alpha:]]',1,level) x
      2  from dual
      3  connect by level <= length('ABC');
    
    X
    ---
    A
    B
    C
    

    Max
    http://oracleitalia.WordPress.com

    Published by: Massimo Ruocchio, February 12, 2010 17:55
    added a second

    Published by: Massimo Ruocchio, February 12, 2010 17:58
    Added the third

  • By subtracting the values in the same column?

    Hello

    How can I subtract two values in the column of the table. EG - there is a table with two columns with date and cumulative turnover.

    Date Total_sales
    ===== =======
    February 23, 68
    24 feb-122
    25 feb-150
    26-Feb-200
    27 feb-223

    I need to know about the date that sales have been maximum. As we can see on 24 - Feb, sales were 54. We do this by subtracting 122-68.
    How can subtract us values in the same column?

    Thank you

    Take a look at the lag() of analytic SQL function which should give you what you need.

    See you soon,.
    Harry

  • By comparing the values in the same column

    Hello

    I have a requirement to do the following:

    The guest of the dashboard has two drop downs as 'Acct number 1' and others as 'Acct number 2 ". Both are the same column from the database. When two different values are chosen from the prompt, it should produce a report with the following 5 colomns, 'ACCT1', 'SALES AMOUNT ACCT1', "ACCT2", "SALES AMOUNT ACCT2", "DIFFERENCE".

    I tried to use a variable of presentation in the command prompt and it fills my column pther having the same value I have input for the first prompt.

    How can I do this? Thanks for your time and your help.

    Hello

    You can try it at the level of the report.

    * Ensure that you have set your guests with different effects. Suppose that the two PVs are acc1 and acc2.
    * Report, add below filter
    Account number is equal to @{acc1} {defualtvalue}
    OR
    Account number is equal to @{acc2} {defualtvalue}
    This will filter the report by the two account numbers.
    * Now apply below in your fx of all columns.

    "ACCT1: ' @{acc1} {defualtvalue}"
    ACCT1 SALES AMOUNT: FILTER (with the HELP of SalesCol (AccountNumber = ' @{acc1} {defualtvalue}'))
    "ACCT2: ' @{acc2} {defualtvalue}"
    ACCT2 SALES AMOUNT: FILTER (with the HELP of SalesCol (AccountNumber = ' @{acc2} {defualtvalue}'))
    DIFFERENCE: FILTER (with the HELP of SalesCol (AccountNumber = ' @{acc1} {defualtvalue}'))-FILTER (with the HELP of SalesCol (AccountNumber = ' @{acc2} {defualtvalue}'))

    Thank you

  • How to exchange the two values in the same column by using the UPDATE?

    Hi all

    I have a table named TEST, and it has a column called A.

    and has values as follows:

    A
    ------
    10
    20
    30
    40
    50

    My question is how to move '10' instead of '20' and '20' instead of '10' in the column? This permutation is only for the first two values (10, 20)

    Note: Must be only in SQL, not in PLSQL.

    Any advice?

    Thanks in advance?

    Update
    test
    the value a = decode(a,10,20,20,10,a);

    Alexander gelin
    http://nimishgarg.blogspot.com/

  • State of color lines based on a value in the same row

    I would like to color report lines based on a value in the same row.

    For bolting with the table 'EMP ':

    I would like job = MANAGER and Red work = CLERK to be green etc etc.

    The other example I found was the possibility of a single color either the nail or the default color.

    I'm looking for a way to do multiple colors.

    Hello

    In the model line, you can use #1 #, 2 # #, etc. to indicate where a field in the report should be released. It doesn't have to be clear - that is to say, you can use it in style tags if you wish.

    So take a query such as:

    SELECT EMPNO,
    ENAME,
    DEPTNO,
    DECODE(DEPTNO, 10, 'green', 20, 'red', 30, 'cyan', 'white') BG_COLOUR
    FROM EMP
    

    You get the column 1 = empno, 2 = ename, 3 = deptno and 4 = bg_colour. In the model line, you can then do:

    Before defining lines (implements the table):

    <table>
    <tr><td>ID</td><td>Name</td><td>Dept</td></tr>
    

    After setting (farm table) lines:

    </table>
    

    Model 1 (used for all lines) line:

    <tr style="background-color:#4#;"><td>#1#</td><td>#2#</td><td>#3#</td></tr>
    

    Then, for each row, the color that has been calculated by using the DECODE function is used in the style tag for color the background of the whole line.

    How to determine the colors, it's you. I used DECODE here, but you can use a field on the DEPT table to hold and use it in your SQL statement.

    Andy

  • I use VISA 5.3 for reading my serial device... my standard equipment is WELL this device will send four messages for only one comand I need all four messages to display at the same time in VISA SERIES READ BUFFER

    I use VISA 5.3 for reading my serial device... my standard equipment is WELL this device will send four messages of single command given to this instrument... and I read messages through VISA series READ... during the race, the VI buffer displays the messages individually...  I need all four messages to display at the same time in VISA SERIES READ BUFFER, in order to find more than this block, I should add from the palette of function.

    Disable character of endpoint detection or make four readings where you concantanate simply the strings together.

  • How to find the value dated the same day in the last year

    I want to know how to find the value dated the same day in the last year.

    I use now, there is the function with the year and the result is

    Year Rev There are Rev
    20113000
    201240003000
    201320004000
    201450002000

    It works but it's not that I want. I'm trying to add the column date (Ex.20140101) in my report but it'snot work.

    I want to show:

    Date Rev There are Rev
    20110112200
    20120112100200
    20130112300100
    20140112500300

    If use (< degree >, < time_dim >. < date >, 365), it works but does not correct 100%

    Please help me...

    Thank you very much

    Check this link cool - bi.com

  • I have a desktop version independent of PS6 - if I download some desktop PS CC application will be this result in all conflicts with both applications on the same desktop computer?

    I have a desktop version independent of PS6 - if I download some desktop PS CC application will be this result in all conflicts with both applications on the same desktop computer?

    Hello

    Yes both licenses will be in conflict and it will activate CS6 with creative cloud.

    You can only use a license on a computer and you can also download the previous version of the creative cloud.

    Concerning

    Gilles marneffe

  • Entries are concatenated in the same column in the excel file

    Hi, I really need help in this emergency. The problem is when I open the spreadsheet file after you run the program below, all the values that I believe are concatenated in the same column. However, there are 2 different analog inputs which I want in 2 different columns. I'm getting the 2 waveforms in the same graph, but when I open the excel file, it seems that the entries are concatenated in the same column. Someone knows how to fix this? Thank you very much.

    Hi Ben 64,.

    Thanks a lot again. I did as you said and I removed the dynamic data and logged files added directly to the results. I'm still checking if the program works as I can branch only when I return to work tomorrow. Tell me if you think it might be able to work this time. Thanks a lot again!

  • Two or more SRID in the same column?

    Hello, I am working with Oracle Spatial, and I have a problem with the SRID.

    I have a column in a table with the SDO_GEOMETRY type and I need to create a Spatial Index to use. But to create a Spatial Index first, I need to insert metadata into the table incluied SRID: for example

    Insert in user_sdo_geom_metadata (table_name, column_name, srid, diminfo) values ('Spatial_Test', 'GEOMETRIA', 8307, SDO_DIM_ARRAY (SDO_DIM_ELEMENT('LONGITUDE',-180,180,0.5), SDO_DIM_ELEMENT ('LATITUDE',-90, 90, 0.5)));

    But I need to work with more than one SRID in this column. Is there anyway to have two or more SRID in the same column? How? and if I insert them, can they give any problem?

    Thank you very much.

    Good bye.

    ZeyKa,

    . . . . You can transform and store all your geometries in SRID 8307 and then turn to their original SRID when you SELECT them.

    -- "Example: Transform geometry from  to SRID 8307 for storage"
    -- "NOTE: Replace  and  with your values"
    INSERT INTO MyTable (ID, geometry,RenderSRID) VALUES (1, SDO_CS.TRANSFORM(,8307), );
    
    -- "Example: Transform stored geometry back to its "
    SELECT SDO_CS.TRANSFORM(geometry,RenderSRID) FROM MyTable WHERE ID = 1;
    

    Kind regards
    Noel

  • put the value into the correct column according to (complex) date

    Hello

    the query I'm doing is very complex (for me, of course), so I need help from you experts...
    I have a table as below

    ID | name | value | Date
    1. Ranieri | 254,2 | 2009-01-21
    2. Jorge | 252,5 | 2009-03-23
    2: Ricardo | 584,5 | 2009-04-21
    5. Fabricio | 478.2 | 2009-02-12
    4 > Felipe | 454,5 | 2009-01-30
    7. Leonardo | 484,6 | 2009-02-15
    8. Marcelo | 854,4 | 2009-02-04

    First of all, I need a query that separates each month on both sides, so for this year, I'm having columns like below

    jan01-15 jan16-31 fev01-15 fev16-28 mar01-15 mar16-31 apr01-15 apr16-30 and so on...

    P.S:this query must deal only with the current year

    So, I need the result is the union of these two tables to be as below

    ID | name | value | Date | jan01-15 | JAN16-31 | fev01-15 | fev16-28 | Mar01-15 | mar16-31 | APR01-15 | Apr16-30 and so on...
    1. Ranieri | 254,2 | 2009-01-21 | 254,2 |
    2. Jorge | 252,5 | 2009-03-23 | 252,5 |
    2: Ricardo | 584,5 | 2009-04-21 | 584,5
    5. Fabricio | 478.2 | 2009-02-12 | 478.2 |
    4 > Felipe | 454,5 | 2009-01-30 | 454,5 |
    7. Leonardo | 484,6 | 2009-02-15 | 484,6 |
    8. Marcelo | 854,4 | 2009-02-04 | 854,4 |

    I 'don' t know if my problem is very clear, but for each row based on the column 'date', I need to put the value in the right column "part of month".

    Is there a way to do this?

    I appreciate a lot of help...

    Thank you

    Published by: Ranieri on 09/02/2009 06:20

    Hello

    This is called a pivot; Here's how you can do:

    SELECT     id
    ,     name
    ,     value
    ,     dt          -- date is not a good column name
    ,     CASE (WHEN TO_CHAR (dt, 'MM') = '01' AND TO_CHAR (dt, 'DD') <= '15' THEN value END)     AS "jan01-15"
    ,     CASE (WHEN TO_CHAR (dt, 'MM') = '01' AND TO_CHAR (dt, 'DD') >= '16' THEN value END)     AS "jan16-31"
    ,     CASE (WHEN TO_CHAR (dt, 'MM') = '02' AND TO_CHAR (dt, 'DD') <= '15' THEN value END)     AS "feb01-15"
    ,     CASE (WHEN TO_CHAR (dt, 'MM') = '02' AND TO_CHAR (dt, 'DD') >= '16' THEN value END)     AS "feb16-28"
    ...
    ,     CASE (WHEN TO_CHAR (dt, 'MM') = '12' AND TO_CHAR (dt, 'DD') <= '15' THEN value END)     AS "dec01-15"
    ,     CASE (WHEN TO_CHAR (dt, 'MM') = '12' AND TO_CHAR (dt, 'DD') >= '16' THEN value END)     AS "dec16-31"
    FROM     ...
    

    If you are looking for pivot, you will have many other examples. Almost all of them combine several rows of the table in a line of output, so they use GROUP BY, an aggregate function, such as MIN or SUM. Your problem doesn't have that.

  • an alphabet by typing in a cell gives drop down suggestions from previous hits on the same column. But how the drop down to choose the suggestions of the other columns and other sheets.

    an alphabet by typing in a cell gives drop down suggestions from previous hits on the same column. But how the drop down to choose the suggestions of the other columns and other sheets.

    Hi mdsavol,

    Your observations are accurate. The 'suggestions' are previous entries in the same column that correspond to what has been entered so far in the active cell. The only direct user control is to activate the function turn on or off in numbers preferences > general.

    There are other ways to include or exclude items of suggestions:

    • To remove typos in the suggestions list, the user must correct the typos in the cell above the active cell. If they are more in the list, they won't be presented as suggestions.
    • To include selections added to the list, the user must enter these suggestions in the individual cells above the active cell and column where they are wanted as suggestions.

    There was a request here a while there is a list of suggestion 'live' similar to those of some websites, which offers a descending list of possible entries as a type in an input box.

    The only way I see to reach a solution similar to what you have asked is to use as many lines at the top of the non-en-tete of the table section to list the items likely to repeat in your table, and then hide the lines. You'll need a list for each column where you want to use this feature with a list previously planted. Existing items will then require a likely hit up to three, then a click to choose from a list small enough to enter a value into a cell. News he will need to enter in full the first time, but after that it will be put on the list and answer the same thing as the terms preseeded.

    While your setting upward (or decide not to do), consider going on the menu of number (in numbers), choosing to provide feedback from numbers and writing a feature in Apple request. Describe what you want. Explain how he could help the average user numbers, and then hope for the best.

    Kind regards

    Barry

  • A target table is in charge of two different sources but the same columns, but a source is one database and another in a flat file.

    We all hope you are doing well.

    I have a business problem to implement in ODI 11 G. It's here. I'm trying to load a target table from two sources that have the same column names. But a source is to the file format and the other is in the Oracle database.

    That's what I think I'll create two mappings in the same interface by using the Union between the sources. But I don't know how the interface would connect to different logical architecture to connect to two different sources.

    Thank you

    SM

    You are on the right track, all in a single interface. Follow these steps

    (1) pull model of your data in the designer of the source file and your table model target to the target pane.

    (2) all relevant columns map

    (3) in the source designer to create a new dataset and choose as the UNION join type (this will create a separate tab in the source designer pane)

    (4) select the new dataset tab in the source designer pane and pull your source oracle table data model in the designer of the source. All columns that are relevant to the target card

    (5) make sure that your staging location is set to a relational technology i.e. in this case the target would be an ideal candidate because it is where the ODI will organize the data from source two files and oracle and perform the UNION before loading to the target

    If you want to watch some pretty screenshots showing the steps above, take a look at http://odiexperts.com/11g-oracle-data-integrator-part-611g-union-minus-intersect/

Maybe you are looking for

  • I forgot the password on ipod

    My son forgot his password for his iPod, I erased the device by iCloud, but I thought that this reset the iPod and return the Hello to the screen to select a new password, but the password screen is still there, then the unit displays on my iMac to e

  • How to type 1/2 characters in El Capitan?

    Please update "how to type the symbol for 1/2". Last reply: October 21, 2014 18:19 by westongallagher is now deprecated as El Capitan does not have 'Special characters' under the 'Edition', only 'Emoji & symbols"menu that does not include fractions.

  • For a laptop of 2b19wm 2000 light speaker output

    Stakeholders on this laptop are so low that I can't really use them.  Is there a different driver which can be downloaded, or is there a solution that I know?  I tried a lot of things without success.  Can anyone help?

  • Pavilion n245se: hp Pavilion n245se bluetooth not found.

    I bought this laptop last month.  Wireless driver for this laptop is Realtek RTL8188EE and then I installed the bluetooth driver Qualcomm Atheros QCA9000 series Bluetooth 4.0 + HS Driver for Microsoft Windows .  I have not found driver bluetooth in D

  • Microsoft solve this centre.will does not work in my windows vista Ultimate edition 32 bit

    i set downloaded Microsoft .but when I go to the for use, he comes up with an error, 0x80092003.a problem prevents to start troubleshooting, continuous .and that my windows vista is not genuine, so that worried me until I checked with the diagnostic