Compare two tables having different number of rows based on 2 columns

Hi,
I am having two tables table a having field1, field2 and table b having fields field1 and field2.
I want those records from table a in which field1 of table a is not matching to field1 of table b and field2 of table b not matching to field2 of table b, but i also want the bifurcation of records as whether field 1 is not matching, field 2 is not matching or both fields 1 & 2 are not matching.
e.g.
table a table b
field1 field2 field1 field2
1 6 12 5
2 5 1 9
13 51 13 51
45 31 99 121
33 45
In this case my output should be
table a
field1 field2 Mtchng_Field
1 6 Field 2 not mtchng
2 5 Field 1 not mtchng
45 31 Feild1 and Field2 both not matching
How would i get my result in the required format.

sql>select * from t1;
N1 N2 
1   6 
2   5 
13  51 
45  31 
33  45 
sql>select * from t2;
N1 N2 
12  5 
1   9 
13  51 
99  121
sql>
select n1,n2,decode(nvl(p1,0)+nvl(p2,0),0,'No match',1,'F2 not match',2,'F1 not match','Match') status
from(
  select n1,n2,(select 1
               from t2 where n1=t1.n1) p1,(select 2
                                       from t2 where n2=t1.n2) p2
from t1);
N1 N2 STATUS 
1   6   F2 not match 
2   5   F1 not match 
13  51  Match 
45  31  No match 
33  45  No match
Message was edited by:
        jeneesh
Message was edited by:
        jeneesh
Some problems...                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

Similar Messages

  • Compare two tables on different server without using Linked Servers

    I have an SSIS package that needs to compare a table to another table on another server.  Then only delete data from the first table if it exists in the second table.  
    I know this is possible with Linked Servers but we are doing away with them in my company.  Is there a way inside of SSIS to do this?
    Oh and I can't use a Data Flow since I don't know the table names or columns at run time (the whole package is dynamic) so I can't map the columns ahead of time.  
    So no MERGE JOIN unless it can be used outside of a Data Flow.  

    You 'll have to use the Data Flow Transformation, thing is, given the constraints, your only option seems to be in generating the package programmatically (using code).
    The type of a program would be a Windows Console. The program would expect a few parameters as the source and target tables along with any necessary connection parameters.
    Then use the merge join (being the easiest to code in) in an Execute SQL Task component.
    Examples of coding packages:
    Samples for creating SSIS packages programmatically
    PS: I can tell you, given your circumstances, at the very minimum the impact on the bottom line of your company is brittle apps accompanied with expensive maintenance which translates into reduced ROI from your tooling.
    Arthur My Blog
    The package is table driven and is a dynamic archival process.  The end user just fills in the framework table and the package archives data from within a certain time range.  The table can exist on any server, any database.  
    That part is done.  The part that still needs to be accomplished is to delete data out of the base table since it is archived.  I need a safe way of doing this and so thought to compare the archive table against the base table.  Because they
    can exist anywhere and can have any name I am not sure how to do this without Linked Servers.  That being said we will not have Linked Servers for this.  
    Is there another safe way to delete data out of the base table?

  • BUG? BRIDGE statement to compare two tables

    Hello,
    I tried to compare two tables in different dbs and remembered a post about Cross Connectin Queries
    http://barrymcgillin.blogspot.com/2010/11/cross-connection-queries.html
    BRIDGE temparb AS "EB05 01"
    (SELECT * FROM arb)
    (SELECT * FROM temparb
    MINUS
    SELECT * FROM arb
    UNION ALL
    (SELECT * FROM arb
    MINUS
    SELECT * FROM temparb
    );I would expect to get the differences between the table arb in my current schema and the table arb alias temparb in the other db. Yet it seems, that in this case only the table in my current schema is read.
    I materialize the BRIDGE table with
    BRIDGE temparb AS "EB05 01"
    (SELECT * FROM arb)and select the value I know to be only in the remote schema
    SELECT id FROM temparb WHERE id = 2562;
    SELECT id FROM arb WHERE id = 2562;Both times I get no result. I even tried to use aliases on the table, same result. Tested in 3.0 and 3.1 EA3.
    Has anyone tried this before?
    Regards
    Marcus

    Hi Marcus,
    Have you tried it without the double quotes around the connection name?
    I have a connection called
    system_local
    which I run the following command in
    drop table testbridge_remote;
    drop table testdbrige;
    create table testbridge(col1 int);
    insert into testbridge values (1);
    insert into testbridge values (2);
    insert into testbridge values (3);
    commit;
    I have a connection called
    Connection Name With Space
    which I run the following in
    drop table testdbrige;
    create table testbridge(col1 int);
    insert into testbridge values (4);
    insert into testbridge values (5);
    insert into testbridge values (3);
    commit;
    Then in the system_local connection/worksheet I can run the following statement
    BRIDGE testbridge_remote as Connection Name With Space(select * from testbridge)
    +(SELECT * FROM testbridge_remote+
    MINUS
    SELECT * FROM testbridge
    +)+
    UNION ALL
    +(SELECT * FROM testbridge+
    MINUS
    SELECT * FROM testbridge_remote
    +);+
    It works ok.
    Returning rows in the remote table not in the local table , and rows in the local table not in the remote table .
    Regards,
    Dermot
    SQL Developer Team.

  • Comparing Two tables with 300k records and update one table

    Could you let me know how to compare two tables having 300k records and update one table.below is the scenario.
    Table Tabl_1 has columns A,B and Tabl_2 has columns B,new_column.
    Column B has same data in both the tables.
    I need to update Tabl_2 in new_column with Tabl_1 A column data by comparing B column in both tables.
    I m trying to do using PLSQL Tables.
    Any suggestion?
    Thanks.

    Hi,
    Whenever you have a problem, please post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) from all tables involved, so that the people who want to help you can re-create the problem and test their ideas.
    Also post the results you want from that data, and an explanation of how you get those results from that data, with specific examples.
    If you're asking about a DML statement, such as UPDATE, the CREATE TABLE and INSERT statements should re-create the tables as they are before the DML, and the results  will be the contents of the changed table(s) when everything is finished.
    Always say which version of Oracle you're using (for example, 11.2.0.2.0).
    See the forum FAQ: https://forums.oracle.com/message/9362002
    ef2019c7-080c-4475-9cf4-2cf1b1057a41 wrote:
    Could you let me know how to compare two tables having 300k records and update one table.below is the scenario.
    Table Tabl_1 has columns A,B and Tabl_2 has columns B,new_column.
    Column B has same data in both the tables.
    I need to update Tabl_2 in new_column with Tabl_1 A column data by comparing B column in both tables.
    I m trying to do using PLSQL Tables.
    Any suggestion?
    Thanks.
    Why are you trying to use PL/SQL tables?  If tabl_1 and tabl_2 are regular database tables, it will be much simpler and faster just to use them.
    Depending on your requirements, you can do an UPDATE or MERGE, either in SQL or in PL/SQL.

  • How to compare two rows from two table with different data

    how to compare two rows from two table with different data
    e.g.
    Table 1
    ID   DESC
    1     aaa
    2     bbb
    3     ccc
    Table 2
    ID   DESC
    1     aaa
    2     xxx
    3     ccc
    Result
    2

    Create
    table tab1(ID
    int ,DE char(10))
    Create
    table tab2(ID
    int ,DE char(10))
    Insert
    into tab1 Values
    (1,'aaa')
    Insert
    into tab1  Values
    (2,'bbb')
    Insert
    into tab1 Values(3,'ccc')
    Insert
    into tab1 Values(4,'dfe')
    Insert
    into tab2 Values
    (1,'aaa')
    Insert
    into tab2  Values
    (2,'xx')
    Insert
    into tab2 Values(3,'ccc')
    Insert
    into tab2 Values(6,'wdr')
    SELECT 
    tab1.ID,tab2.ID
    As T2 from tab1
    FULL
    join tab2 on tab1.ID
    = tab2.ID  
    WHERE
    BINARY_CHECKSUM(tab1.ID,tab1.DE)
    <> BINARY_CHECKSUM(tab2.ID,tab2.DE)
    OR tab1.ID
    IS NULL
    OR 
    tab2.ID IS
    NULL
    ID column considered as a primary Key
    Apart from different record,Above query populate missing record in both tables.
    Result Set
    ID ID 
    2  2
    4 NULL
    NULL 6
    ganeshk

  • Merging two separate Tables into One - different number of rows

    Hi. I have a problem. Oracle 10.2.0.4.0
    7 years ago when i was learning how to query my teacher told me that is possible to creata some kind of report where results could be combined in 1 view(report) witch in different variable could have different number of rows.
    I just remember that there is needed to use group by function and some join?
    Please help
    In link there is a sample sample view
    I need to combine Table A using D variable with Table B to become Wynik(result)
    Tables create
    CREATE TABLE "TABELA_A" ( A NUMBER,B NUMBER,C NUMBER,D NUMBER ) ;
    INSERT INTO "TABELA_A" (A, B, C, D) VALUES (123, 1, 70, 999)
    INSERT INTO "TABELA_A" (A, B, C, D) VALUES (123, 2, 80, 999)
    INSERT INTO "TABELA_A" (A, B, C, D) VALUES (234, 1, 100, 111)
    INSERT INTO "TABELA_A" (A, B, C, D) VALUES (456, 1, 10, 222)
    CREATE TABLE "TABELA_B" ( D NUMBER,E VARCHAR2(255),F NUMBER ) ;
    INSERT INTO "TABELA_B" (D, E, F) VALUES (999, 'A', 1);
    INSERT INTO "TABELA_B" (D, E, F) VALUES (999, 'B', 1);
    INSERT INTO "TABELA_B" (D, E, F) VALUES (999, 'B', 3);
    INSERT INTO "TABELA_B" (D, E, F) VALUES (999, 'C', 1);
    INSERT INTO "TABELA_B" (D, E, F) VALUES (111, 'A', 1);
    INSERT INTO "TABELA_B" (D, E, F) VALUES (111, 'C', 2);
    And to become result - in picture
    [http://i303.photobucket.com/albums/nn153/katanbutcher/pytanko.jpg?t=1306152636]
    Thank's for help
    Edited by: 860710 on 2011-05-23 05:42
    Edited by: 860710 on 2011-05-23 05:54
    Edited by: 860710 on 2011-05-23 06:07

    Maybe if you follow the instructions mention in this post you may get some help. For example, I wouldn't try to click on the link provided by you.
    SQL and PL/SQL FAQ
    Regards
    Raj

  • Compare two table columns

    hi all,
    i am using db10g.
    my task is to compare two table's data for example
    table1 is having
    col1 col2 col3
    a b c
    e f g
    table2 is
    col4 col5 col6
    a e c
    e f g
    so i have to compare col1 first record and col4 first record.
    in otherwords first table first column first item with second table first table first value and so on.
    for ex: a= a
    b= e
    c= c etc
    how can i acheive this?
    both are database tables.
    i cannot use minus function because data types are different for the corresponding column
    how can i compare it?
    Thanks..
    Edited by: user13329002 on Jan 13, 2011 1:09 AM

    Hi,
    Try this
    select
        case
            when a.col1 = b.col1 then 'EQUAL' else 'NOT EQUAL' end column1,
        case
            when a.col2 = b.col2 then 'EQUAL' else 'NOT EQUAL' end column2,
        case
            when a.col3 = b.col3 then 'EQUAL' else 'NOT EQUAL' end column3 
    from (select col1,col2,col3,rownum rn from table1) a,
                                (select col1,col2,col3,rownum rn from table2) b where a.rn = b.rn Try to see how you order the rows of the two tables.
    cheers
    VT

  • Different number of rows for different columns in JTable

    hi
    I need to create a JTable with different number of rows for different columns...
    Also the rowheight should be different in each column...
    say there is a JTable with 2 columns... Col1 having 5 rows and column 2 having 2 rows...
    The rowHeight in Col2 should be an integer multiple of Rowheight in Col1
    how do I do this ??
    can anybody send me some sample code ?????
    thanx in advance

    How about nesting JTables with 1 row and many columns in a JTable with 1 column and many rows.
    Or you could leave the extra columns null/blank.
    You could use a GridBagLayout and put a panel in each group of cells and not use JTable at all.
    It would help if you were more specific about how you wanted it to appear and behave.

  • Compare two string in different line in textarea

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class TextCounterPanel extends JPanel {
    private JTextArea textInput;
    private JLabel lineCountLabel;
                        public TextCounterPanel() {
                        setBackground(Color.DARK_GRAY);
                        textInput = new JTextArea();
                        textInput.setBackground(Color.WHITE);
                        JPanel south = new JPanel();
                        south.setBackground(Color.DARK_GRAY);
                        south.setLayout( new GridLayout(4,1,2,2) );
                             JButton countButton = new JButton("Process the Text");
                             countButton.addActionListener( new ActionListener() {
                                  public void actionPerformed(ActionEvent evt) {
                                       processInput();
                             south.add(countButton);
                        lineCountLabel = new JLabel(" Number of lines:");
                        lineCountLabel.setBackground(Color.WHITE);
                        lineCountLabel.setForeground(Color.BLUE);
                        lineCountLabel.setOpaque(true);
                        south.add(lineCountLabel);
                        setLayout( new BorderLayout(2,2) );
                        setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
                        JScrollPane scroller = new JScrollPane( textInput );
                        add(scroller, BorderLayout.CENTER);
                        add(south, BorderLayout.SOUTH);
    public void processInput() {
    String text,vin; // The user's input from the text area.
    text = textInput.getText();
    vin =text.substring(25,42);
    lineCountLabel.setText(" vin: " + vin);
    } // end class TextCounterPanel
    How can I compare two string in different line in text area

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class TextCounterPanel extends JPanel {
    private JTextArea textInput;
    private JLabel lineCountLabel;
    public TextCounterPanel() {
    setBackground(Color.DARK_GRAY);
    textInput = new JTextArea();
    textInput.setBackground(Color.WHITE);
    JPanel south = new JPanel();
    south.setBackground(Color.DARK_GRAY);
    south.setLayout( new GridLayout(4,1,2,2) );
    JButton countButton = new JButton("Process the Text");
    countButton.addActionListener( new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
    processInput();
    south.add(countButton);
    lineCountLabel = new JLabel(" Number of lines:");
    lineCountLabel.setBackground(Color.WHITE);
    lineCountLabel.setForeground(Color.BLUE);
    lineCountLabel.setOpaque(true);
    south.add(lineCountLabel);
    setLayout( new BorderLayout(2,2) );
    setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
    JScrollPane scroller = new JScrollPane( textInput );
    add(scroller, BorderLayout.CENTER);
    add(south, BorderLayout.SOUTH);
    public void processInput() {
    String text,vin; // The user's input from the text area.
    text = textInput.getText();
    vin =text.substring(25,42);
    lineCountLabel.setText(" vin: " + vin);
    } // end class TextCounterPanel

  • RDL report(2008).Want to display different number of rows from second page onwards than the first page.

    I have used pagination to display the report data.I have used page break.I want to display 10 records on first page and from second page onwards I want to display 25 records on all remaining pages.
    I followed this link to show 25 records for all page. "http://www.sqlchick.com/entries/2010/9/11/displaying-fixed-number-of-rows-per-ssrs-report-page.html". Now suggest me how display 10 records only on first page having 25 from second page onwards.

    Hi mukesh_harkhani,
    According to your description, you want to insert page break for different number of rows, display 10 rows on the first page and 25 rows on the following pages. After testing the issue in my SQL Server Reporting Services 2008 environment, we can use the
    method below to achieve your requirement:
    In your scenario, right-click the group which contains the expression: =CEILING(RowNumber(Nothing)/25) to open the Group Properties dialog box.
    Modify the original expression to the following in the Group on textbox:
    =Floor((RowNumber(Nothing)+14)/25)
    If there are any other questions, please feel free to ask.
    Thanks,
    Katherine Xiong
    Katherine Xiong
    TechNet Community Support

  • Join two table in different instance

    Hi,
    I have two table in different instance .
    IMEI in instance A
    RCA_SMART_CARD in instance B
    Below is the desc table :
    SQL> desc RCA_SMART_CARD;
    Name Null? Type
    N_CARD_ID NOT NULL NUMBER(10)
    C_CARD_SERIAL_NUMBER NOT NULL VARCHAR2(20)
    C_SIM_MSISDN VARCHAR2(20)
    C_SIM_IMSI VARCHAR2(20)
    C_LINKED_CARD VARCHAR2(20)
    N_PRO_IDENTIFIER NOT NULL NUMBER(4)
    C_CARD_TYPE VARCHAR2(1)
    N_SIM_STATE NUMBER(1)
    N_EEPROM_SPACE_LEFT NUMBER(9)
    N_VOLATILE_SPACE_LEFT NUMBER(9)
    N_NONVOLATILE_SPACE_LEFT NUMBER(9)
    N_CARD_OPTI NOT NULL NUMBER(15)
    N_PRODUCT_ID NUMBER(10)
    D_CREATION_DATE DATE
    D_MODIFICATION_DATE DATE
    D_STATUS_MODIFICATION_DATE DATE
    SQL> desc IMEI;
    Name Null? Type
    MSISDN NOT NULL VARCHAR2(20)
    IMEI NOT NULL VARCHAR2(16)
    DATE_MOD NUMBER(13)
    IMSI VARCHAR2(18)
    ICCID VARCHAR2(20)
    T_PROF RAW(20)
    EXTRA_DATA VARCHAR2(100)
    If I want to join two table together .
    I want to search the number of record in IMEI that have N_SIM_STATE =1 in RCA_SMART_CARD .
    The MSISDN in IMEI is equal to C_SIM_MSISDN in RCA_SMART_CARD .
    How can I do and what is the sql statment ??
    Please advice .

    First you need to decide, from where you want to execute the query.
    Let us assume it is instance A(as per your example).
    Then what you need to do is:
    1.     Create database link to instance B
    *[url http://www.stanford.edu/dept/itss/docs/oracle/10g/server.101/b10759/statements_5005.htm]Here* is the semantics.
    2.     After creating the database link to instance B, you can achieve the result set by running the below query:
            SQL> select count(*) from IMEI t1 join RCA_SMART_CARD@<db_link_name> t2 on t1.MSISDN = t2.C_SIM_MSISDN where t2.N_SIM_STATE = 1
            Hope it helps!
    Cheers,
    AA

  • How do you join two tables from different Oracle schemas using a subquery

    I am trying to join two tables from different Oracle schemas using a subquery. I can extract data from each of the tables without a problem. However, when I combine the select statements using a subquery I get the Oracle error *'ORA-00936: missing expression'*. Since each SELECT statement executes on its own without error I don't understand what is missing. The result set I am trying to get is to match up the LINE_ID from PDTABLE_12_1 in schema DD_12809 with the MAT_DESCRIPTION from table PDTABLE_201 in schema RA_12809.
    The query is as follows:
    sql = "SELECT [DD_12809].[PDTABLE_12_1].LINE_ID FROM [DD_12809].[PDTABLE_12_1] JOIN " _
    + "(SELECT [RA_12809].[PDTABLE_201].MAT_DESCRIPTION " _
    + "FROM [RA_12809].[PDTABLE_201]) AS FAB " _
    + "ON [DD_12809].[PDTABLE_12_1].PIPING_MATER_CLASS = FAB.PIPING_MATER_CLASS"
    The format of the query is copied from a SQL programming manual.
    I also tried executing the query using a straight JOIN on the two tables but got the same results. Any insight would be helpful. Thanks!
    Edited by: user11338343 on Oct 19, 2009 6:55 AM

    I believe you are receiving the error because you are trying to JOIN on a column that doesn't exist. For example you are trying to join on FAB.PIPING_MATER_CLASS but that column does not exist in the subquery.
    If you want to do a straight join without a subquery you could do the following
    SELECT  DD_12809.PDTABLE_12_1.LINE_ID
    ,       FAB.MAT_DESCRIPTION
    FROM    DD_12809.PDTABLE_12_1
    JOIN    RA_12809.PDTABLE_201    AS FAB ON DD_12809.PDTABLE_12_1.PIPING_MATER_CLASS = FAB.PIPING_MATER_CLASS  HTH!

  • Trouble with the SQL smt to :list tables having more than 1000 rows

    Please I trying to list only tables having more than 1000 rows, but the sql stmt below doesn't work, can someone gives me a tips
    select table_name from user_tables where table_name in ( select table_name from user_tables where rownum > 1000 ) : The result is no rows!
    But I know that I have at lest 50 tables having more than 1000 rows
    Thanks a lot for the help

    If your tables are reasonably analyzed, then you can simply query:
    SELECT table_name,
           num_rows
      FROM user_tables
    WHERE num_rows >= 1000This will give you quite a reasonable estimate.
    Otherwise you have to go for dynamic sql or use the data dictionary to help you generate suitable scripts ....

  • Compare two tables and log the difference

    Hi,
    I want to compare two tables and log the difference in new table with the fields as (old value,new value, column name). The column name should be the changes value column

    Can you show an example?
    SELECT Foo.*, Bar.*
       FROM Foo
            FULL OUTER JOIN
            Bar
            ON Foo.c1 = Bar.c1
               AND Foo.c2 = Bar.c2
               AND Foo.cn = Bar.cn
     WHERE Foo.key IS NULL 
        OR Bar.key IS NULL; 
    Best Regards,Uri Dimant SQL Server MVP,
    http://sqlblog.com/blogs/uri_dimant/
    MS SQL optimization: MS SQL Development and Optimization
    MS SQL Consulting:
    Large scale of database and data cleansing
    Remote DBA Services:
    Improves MS SQL Database Performance
    SQL Server Integration Services:
    Business Intelligence

  • How to compare two database on different servers

    hi,
    I want to compare two database on different servers in oracle.first i want to ckechk thier structures and then data.i m using oracle 10.2.0.plz help me out
    Thanks in advance

    In terms of structures, there are various tools out there that can do this, such as TOAD which can do a database compare.
    In terms of data comparisons, that will depend on what sort of results you are expecting to see. I'm sure some tools will do it for you, but it can be just as easy to write your own comparison using a database link from one database to the other.

Maybe you are looking for

  • Error message when sending mail

    I am using a G3 tower runnig OSX 10.3.9 with 768 megs of memory. I am using Mac Mail and had tried to send an email that I had accidentally attached the wrong file (12 megs). I realized my mistake after I hit send and I tried to delete it from the ou

  • IPod Touch 3rd Gen Dead?

    My iPod Touch is not recognized by iTunes. I've tried hard resetting it multiple times (holding home and sleep button), but the screen remains blank. This problem surfaced a couple months ago and I have been letting it "rest" for quite some time. I c

  • The backup disk image "/Volumes/Data/Macintosh.sparsebundle" is already in use.

    Now what, how do you fix this???

  • User exit for four quaters of a year

    Hi All, I need to write an user exit to fetch the Quater1(3 months) to Quater4 from a Calyear. If the user enters 2008 then i want to get the sales for the 4 quaters for  an year in separate columns. I think of creating 4 restrcited KFs with the sale

  • Sick of syncing problems

    hey apple. get your act together! the one huge plus about having everything in the apple ecosystem is how it all synced seamlessly. not anymore. every other attempt at syncing music will either 1. lose the album art, 2. not update checked playlists e