Compare three columns within one table. Help!

Hi All
I had a post up about this yesterday but I didnt have much luck and im still really stuck on this so if there is anyone who has any idea please help me.
I have three columns A,B,C within the one table. Column A may contain a value that is in Column B so if this is the case I need to enusre that it is not brought back in the query. I have tried using a minus function but it is not working very well. Probably because i am doing it wrong.
So basically I need the query to
Compare
(A) Column A with Column B - If a value is present in column B I want to minus it/remove it from Column B
(B) Column A with Column C - If a value is present in column C I want to minus it/remove it from Column B
(C) Column B with Column C - If a value is present in column C I want to minus it/remove it from Column B
OUTPUT AT PRESENT
COL A COLB COLC
12345 45666 78888
45666 78888 66666
66662 12345 37891
Desired Output
COL A COLB COLC
-------- 45666 78888
-------- -------- 66666
66662 12345 37891
Any help with this would be really appreciated
Thanks

SQL> select * from poles;
        P1         P2         P3
       123        101        123
       456        123        456
       789        456        999
SQL> select decode((select count(*)
  2                from poles
  3                where p2 = p.p1
  4                or p3 = p.p1),0,p1) p1,
  5          decode((select count(*)
  6                from poles
  7                where p3 = p.p2),0,p2) p2,p3
  8  from poles p;
        P1         P2         P3
                  101        123
                             456
       789                   999                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

Similar Messages

  • Need to compare values in two columns of one table against values in two columns in another table

    Hi, as the title reads, I'm looking for an approach that will allow me to compare values in two columns of one table against values in two columns in another table.
    Say, for instance, here are my tables:
    Table1:
    Server,Login
    ABCDEF,JOHN
    ABCDEF,JANE
    FEDCBA,SEAN
    FEDCBA,SHAWN
    Table2:
    Server,Login
    ABCDEF,JOHN
    ABCDEF,JANE
    FEDCBA,SHAWN
    In comparing the two tables, I'd like my query to report the rows in table1 NOT found in table2. In this case, it'll be the 3rd row of table one:
    Server,Login
    FEDCBA,SEAN
    Thanks.

    create table Table1([Server] varchar(50), Login varchar(50))
    Insert into Table1 values ('ABCDEF','JOHN'),('ABCDEF','JANE'),('FEDCBA','SEAN'),('FEDCBA','SHAWN')
    create table Table2([Server] varchar(50), Login varchar(50))
    Insert into Table2 values ('ABCDEF','JOHN'),('ABCDEF','JANE'), ('FEDCBA','SHAWN')
    select [Server] ,Login from Table1
    Except
    select [Server] ,Login from Table2
    select [Server] ,Login from Table1 t1
    where not exists(Select 1 from Table2 where t1.[Server] = t1.[Server] AND Login=t1.Login)
    drop table Table1,Table2

  • How to move a column within a table?

    Hi All!
    Is it possible in Oracle 9i to move a column within a table? For instance, I have a table
    ID1 ID2 ID3 SRC_ID1 SRC_ID2 SRC_ID3
    and I want to add one more column ID4. So, I can do it like this:
    ALTER TABLE MY_TABLE
    ADD ID4 NUMBER(18);
    In this case, the column will be added to end of the table but I want to move the column on the position after ID3. So, my question is, is it possible to move a column within a table? And if yes, how I can do it?.
    Any help will be appreciate.
    With best regards,
    Andrej Litowka.

    Hi,
    Actually speaking it makes no difference where a column is placed in the table. The select clause is the one that decides on how to display the data. But still if you want to store it next to a particular column then there are 2 options...
    1. Rename <tname> to <tname_1>;
    Create view <tname> as select <cols> from <tname_1>;
    The above option is useful in case of selects. In case of DMLs it will have to do a little more work by going and firing the same on the actual underlying table.
    2. Rename <tname> to <tname_1>;
    Create table <tname> (<new col order with the new col>);
    Insert into <tname> (select <col order> from <tname_1>);
    This option seems to be better than the first one.
    Hope this helps you.
    Regards,
    DJ

  • Hide a column in one table view but show it in another

    I need to hide a column in one tables view but show it in another table view in the same report. Is this possible?
    Any help on this is appreciated.
    Edited by: VNC on Jan 11, 2010 9:26 AM

    Hi VNC,
    If you're crafty with javascript, you could probably write a script that would set the CSS property DISPLAY to NONE to hide the column in the table view you didn't want but leave it there in the view you did want.
    -Joe

  • How can I make my query to compare only columns of two tables... not the storage information?

    11GR2
    =-----------------------------------
    I am using below querry to compare two table that has same name, under two different users... But after making storage information false like below and  if the storage information is different on column level than it create "Alter modify " statements for the column ... How can I make my query to compare only columns of two tables... not the storage information?
    begin
    DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'PRETTY', TRUE);
    DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'SQLTERMINATOR',TRUE);
    DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'SEGMENT_ATTRIBUTES', FALSE);
    DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'STORAGE', FALSE);
    DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'TABLESPACE',FALSE);
    DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'CONSTRAINTS',FALSE);
    DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'REF_CONSTRAINTS',FALSE);
    DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'CONSTRAINTS_AS_ALTER',FALSE);
    End;
    select REGEXP_REPLACE(dbms_metadata_diff.compare_alter('TABLE','TABLE_NAME_A','TABLE_NAME_A','USER1','USER2'),('USER1...'),'', 1, 0, 'i') from dual

    I am using below querry to compare two table that has same name, under two different users... But after making storage information false like below and  if the storage information is different on column level than it create "Alter modify " statements for the column ... How can I make my query to compare only columns of two tables... not the storage information?
    If you want help you have to SHOW us what you are doing and how you are doing it; you can't just try to tell us in your own words.
    We can't see your computer screen.

  • Comparing data size in one table to column widths in another table

    I have data in a table that has a large number of columns, many of them nvarchar of varying widths.  Im trying to take that data and insert it into another table but Im getting the warning message about string or binary data being truncated.  I
    suspect there is a field somewhere that is not large enough for the data.  However, I run across this often enough I would like to come up with a better solution than just eyeballing the data.
    I found this example
    http://www.sqlservercentral.com/Forums/Topic1115499-338-2.aspx
    (credit goes to poster in the linked thread above)
    Select columns
    into #T
    from MyDataSource;
    select *
    from tempdb.sys.columns as TempCols
    full outer join MyDb.sys.columns as RealCols
    on TempCols.name = RealCols.name
    and TempCols.object_id = Object_ID(N'tempdb..#T')
    and RealCols.object_id = Object_ID(N'MyDb.dbo.MyTable)
    where TempCols.name is null -- no match for real target name
    or RealCols.name is null -- no match for temp target name
    or RealCols.system_type_id != TempCols.system_type_id
    or RealCols.max_length < TempCols.max_length ;
    Why a full outer join ?  Why not just a left join, since I really only want to see the matches on my source table?
    When Im running this against the table im interested in, it doesnt seem to find matches between my target table and my temp table 

    As an outer join of any type, that query won't work well.  For example, suppose you do a left join.  So the query begins by getting every row from tempdb.sys.columns (whether it is in #T or not).  Consider a row for a column which is not in
    #T, you look for matches for rows in Mydb.sys.columns ON
    on TempCols.name = RealCols.name
    and TempCols.object_id = Object_ID(N'tempdb..#T')
    and RealCols.object_id = Object_ID(N'MyDb.dbo.MyTable)
    Notice that since the row you are considering is NOT a column in #T, the second part of the ON condition is not true, so the whole ON condition will not be true.  But this is a left join.  So the join keeps this row with NULL's in the columns coming
    from the RealCols table.  Then you do the where condition, but the connections are all OR and one of the conditions is RealCols.name is null (which it is because there was no match), your output will include a row for this column in tempdb even though
    this column is not in #T.  So if you use a left join, the output of this query will include a row for every column in every table in tempdb not named #T.
    Similarly, if you do a right join, you get a column for every row of every table in MyDb which is not a column in dbo.MyTable.
    And a full join (which you are doing above) will return a row for every column in every table in both tempdb and MyDb.
    This query will sort of work if you make it an inner join.  But even then it won't find every possible cause of string or binary truncation.  For example, you are doing RealCols.max_length < TempCols.max_length.  But in sys.columns, if
    you have a varchar(max) column, max_length is stored as -1.  So if a column in RealCols is varchar(50) and the same column in TempCols is varchar(max), that column will not show up as an exception, but of course, you can get truncation if you attempt
    to store a varchar(max) in a varchar(50).
    I would run a query more like
    Create Table FooX(a int, b varchar(20), c int, d varchar(20), e int, f varchar(20), g decimal(4,0));
    Select a, b, 1 as x, 'abc' as y, Cast('' as varchar(max)) As f, Cast(25.1 as decimal(3,1)) as g
    into #T
    from FooX;
    Select 'In Real, not in or different in Temp' As Description, name, column_id, system_type_id, max_length, precision, scale, collation_name From sys.columns Where object_id = Object_ID(N'FooX')
    Except Select 'In Real, not in or different in Temp' As Description, name, column_id, system_type_id, max_length, precision, scale, collation_name From tempdb.sys.columns Where object_id = Object_ID(N'tempdb..#T')
    Union All
    Select 'In Temp, not in or different in Real' As Description, name, column_id, system_type_id, max_length, precision, scale, collation_name From tempdb.sys.columns Where object_id = Object_ID(N'tempdb..#T')
    Except Select 'In Temp, not in or different in Real' As Description, name, column_id, system_type_id, max_length, precision, scale, collation_name From tempdb.sys.columns Where object_id = Object_ID(N'FooX')
    Order By name, Description;
    go
    Drop Table #T
    go
    Drop Table FooX
    The output of that is
    In Real, not in or different in Temp c 3 56 4 10 0 NULL
    In Real, not in or different in Temp d 4 167 20 0 0 SQL_Latin1_General_CP1_CI_AS
    In Real, not in or different in Temp e 5 56 4 10 0 NULL
    In Real, not in or different in Temp f 6 167 20 0 0 SQL_Latin1_General_CP1_CI_AS
    In Temp, not in or different in Real f 5 167 -1 0 0 SQL_Latin1_General_CP1_CI_AS
    In Real, not in or different in Temp g 7 106 5 4 0 NULL
    In Temp, not in or different in Real g 6 106 5 3 1 NULL
    In Temp, not in or different in Real x 3 56 4 10 0 NULL
    In Temp, not in or different in Real y 4 167 3 0 0 SQL_Latin1_General_CP1_CI_AS
    From which you can quickly see that the differences are c, d, and e are in the real table and not the temp table, f is in both tables but the max_length is different, g is in both table, but the precision and scale are different, and x and y are in the temp
    table, but not the real table.
    Tom

  • How do i change the number of columns within a text box?  I need to go from three columns to one.

    How do I change the number of columns within a text box?  I need to go from 3 columns to 1.  The insert column is highlighted and column break does not work for this.

    Pages '09 does not seem to allow layout breaks in Textboxes.
    Pages 5.2 has the same limitation.
    Use multiple linked Textboxes in Pages '09 to get the layout you want or create your layout in a Word Processing template in the document layer.
    Peter

  • Compare two columns between two tables

    Hi all,
    I have two separate datasets in Table 1 and Table 2.
    Both datasets have Column A First name and Column B Last name in common.
    The data in the subsequent columns is different.
    Some of the people listed in each table are the same.
    I want to delete the people from Table 1 who are in table 2.
    If I run a formula like this =COUNTIF(Table 1 :: A2:A200, A2) I can return a 1 or 0, use conditional formatting to colour that cell, sort the table and delete all the rows containing 1.
    However that only checks for First Name.
    What I need to do is check if First name AND Last name in Table 1 are the same as First Name AND Last Name in Table 2 - and then return a True of False (1 or 0 or anything) in a separate column.
    Please help.
    Thanks in advance.
    M.

    Moich wrote:
    Presumably this lets me compare multiple values across multiple tables also?
    Moich,
    As you will see as you become familiar with this function, it can only count rows in one table. The comparison (criteria) values can come from anywhere, but what you are counting are "records" that meet the multiple criteria. A record is one row. I think that was your question, but I'm not positive.
    I can't seem to find how to insert formulas into the conditional rules.
    You'll have to put the formulas elsewhere and refer to them with simple comparisons.
    Jerry

  • How I compare the columns of 2 tables

    Hi, what's the query I use to compare all the columns of 2 tables, or how I use the result of COLUMN_NAME in the DBA_TAB_COLS?
    Tnks for helping...
    Mnk

    I have this select:
    SELECT D.COLUMN_NAME AS CFINAL
    FROM DBA_TAB_COLS D,
    (SELECT A.COLUMN_NAME AS C1
    FROM DBA_TAB_COLS A
    WHERE (A.OWNER LIKE '%TEL_USJ07%' AND
    A.TABLE_NAME = 'BOLETOS')) F
    WHERE D.COLUMN_NAME = F.C1
    AND D.OWNER LIKE '%TEL_USJ07%'
    This will return the column's name where tabel_name LIKE BOLETOS.
    I want use this result in another select where the table have this columns and more, but I want to see all the results in the second table where the columns came from the first select.
    For example: the first select bring to me the columns: cod_tipo_bol and cod_tipo_corte, I want to see in the second select only the results of this 2 columns.
    THIS IS THE COLUMNS OF THE SECOND TABLE I WANT TO USE:
    cod_tipo_bol
    cod_frente_corte
    num_carga
    cod_caminhao
    cod_motorista
    lib_numero
    cod_enc_frente
    cod_div2
    cod_div3
    cod_div4
    bol_data
    cod_carregadora1
    cod_oper_carreg1
    cod_carregadora2
    cod_oper_carreg2
    cod_colhedora1
    cod_oper_colhed1
    cod_colhedora2
    cod_oper_colhed2
    cod_colhedora3
    cod_oper_colhed3
    cod_trat_reb
    cod_oper_trat_reb
    cod_trat_transb1
    cod_oper_transb1
    cod_trat_transb2
    cod_oper_transb2
    cod_trat_transb3
    cod_oper_transb3
    cod_julieta_sm_reboque
    bol_status
    --------

  • Comparing clob columns in two tables

    I need to copy the clob data from one table to another. However, I need to remove duplicates while doing so. When I run the following query, it loads all of the rows, whether they are duplicates or not.
    INSERT INTO L_COMMENTS
    SELECT comments_key.NEXTVAL, 'ASSOCIATECOMMENTS', ASSOCIATECOMMENTS
    FROM TABLE1 A
    WHERE ASSOCIATECOMMENTS IS NOT NULL
    AND NOT EXISTS
    (select 1
    from L_COMMENTS C
    where COMMENT_TYPE = 'ASSOCIATECOMMENTS'
    AND dbms_lob.compare(C.COMMENTS, A.ASSOCIATECOMMENTS) = 0)
    Any idea on how to do this load? I have several similar loads to do.

    If I did that, for any column that has a duplicate, neither one would be included.

  • How to insert records with LONG RAW columns from one table to another

    Does anybody know how to use subquery to insert records with columns of LONG RAW datatype from one table to another? Can I add a WHERE clause in the subquery statement? Thanks.

    Insert into ... Select statements are not supported for long or long raw. You will have to either use PL/SQL or convert your long raw to blobs.

  • Create ViewCriteria comparing two columns from same table

    Does anyone know how I can create a ViewCriteria where clause that compares two columns from the same table?
    For example if I had two integer columns (MINSAL and MAXSAL) and wanted to see if they are equal. I would normally do the following SQL below.
    SELECT * FROM EMPL
    WHERE MINSAL = MAXSAL

    It works, but it is not ideal.
    Setup a Transient column that performs a groovy evaluation of MINSAL=MAXSAL and then my ViewCriteria evaluates the column to true and I set Query Execution Mode to Both.

  • Three Columns into One?

    I have a list of about 1000 items. Currently they are in three columns. Is there an more automated way of making one single column rather than doing this manually with the return/enter button and arrow keys?
    (Using Pages Ver 1.0.2)

    I presume you have three columns made by tabbing.
    Yes.
    Use the Find dialog and search for a tab, replace
    with just a return...
    TIP: show invisibles and select the tab and or
    return symbols so you can paste into the seach(find)
    dialog.
    How do you mean show invisibles? I set the whole OS to show invisible files. That doesn't affect Pages. How can I copy tab and return symbols?
    iBook G4   Mac OS X (10.4.4)  

  • Query for comparing two columns in two tables

    Hai, all,
    I have following two tables and columns, I need to get the rows (all columns) from scott.cp_ip which are not ( the same value present in t1 and also in t2).
    Table scott.cp_ip column cp_ip.vrno = Table scott.cb1 column cb1.cb_vnno
    I used the following query and did not get the correct result:
    select * from scott.cp_ip where cp_ip.vrno in (select cb1.cb_vnno from scott.cb1);
    Can you please tell how it can be solved?
    Thanks in advance.
    Rcs
    SQL> desc scott.cb1;
    Name Null? Type
    ID NUMBER
    SUPCODE NUMBER
    SUPLNAME VARCHAR2(100)
    NAME VARCHAR2(100)
    ITEMCODE VARCHAR2(10)
    RECDOC NUMBER
    RECDATE VARCHAR2(10)
    TOTVALUE NUMBER
    QTY NUMBER
    CB_IPNO NUMBER
    CB_VNNO NUMBER
    CB_VDT VARCHAR2(10)
    CB_AMT NUMBER
    SQL> desc scott.cp_ip;
    Name Null? Type
    ID NUMBER
    VRNO NUMBER
    CODE VARCHAR2(50)
    HEAD VARCHAR2(255)
    PARTI VARCHAR2(255)
    CR NUMBER
    SQL> select * from scott.cp_ip
    2 intersect
    3 select * from scott.cb1;
    select * from scott.cp_ip
    ERROR at line 1:
    ORA-01789: query block has incorrect number of result columns
    SQL> (select * from scott.cp_ip
    2 minus
    3 select * from scott.cb1)
    4 union all
    5 (select * from scott.cb1
    6 minus
    7 select * from scott.cp_ip);
    (select * from scott.cb1
    ERROR at line 5:
    ORA-01789: query block has incorrect number of result columns
    SQL> select * from scott.cp_ip where cp_ip.vrno in (select cb1.cb_vnno from scott.cb1);
    result=instead of 1408 rows, 1481 rows (excess 73 rows) getting!
    ----------

    Instead of SELECT * write only the column that you want to compare. I guess that would be cb1.cb_vnno and cp_ip.vrno.

  • Compare two fields of one table in selection.

    Dear Experts,
    I want to compare two fields of a table and make selection according to this.
    Ex: simply i want to select the rows from table mard where mard-labst>mard-insme. Could you write me the code to make this selection.

    Yes, Thomas,
    Its working:
    Sample code:
    data: begin of itab occurs 0,
          matnr type matnr,
          ernam type ernam,
          end of itab.
    select matnr ernam
           into table itab
           from mara where brgew > mara~ntgew.
    loop at itab.
      write: itab-matnr.
    endloop.
    Or,
    Try with inner join once.
    Its working: Sample code.
    data: begin of itab occurs 0,
          matnr type matnr,
          ernam type ernam,
          end of itab.
    select p~matnr q~ernam
           into table itab
           from mara as p
           inner join mara as q
           on p~brgew > q~ntgew.
    loop at itab.
      write: itab-matnr.
    endloop.
    Thanks,
    Naveen.I

Maybe you are looking for