To find matching records across two tables

Hi folks,
I have a question regarding modifying the query. I am trying to compare the data between two tables, there are matching records in two tables, but are not in the same order.  How to modify the query so that it finds the matching record between  the two tables?
Here is my code
  loop at itab_A.
      read table itab_B with key
             employeenumber = itab_zmgr_training-empid binary search.
          if sy-subrc = 0.
Any thoughts?
Thanks,
Sk
Here I am getting the return value as '4'

I have tried all that, but why is it that while reading a record from itab_B I see no record in the header line,
Let me paste the entire code of what I am trying to achieve.
data: itab_A like A occurs 0 with header line.
data: itab_B like B occurs 0 with header line.
some validation.....
loop at itab_A.
read table  itab_B with key  employee number = empid binary search.
  if sy-subrc =0.
per form validation.
else.
perform validation.
endif.
endloop.
My observation: while reading recrod from itab_B the header line is clear, no record is read here hence sy-subrc = 0.
How can I correct this? Since it is not reading any record from itab_B no matching record is found.
Any thoughts would be really helpful.
Thanks,
Santhosh.

Similar Messages

  • Find matching records from two tables, useing three key fields, then replace two fields in table1 from table2

    I have two tables - table1 and table2 - that have the exact same schema. There are three fields that can be used to compare the data of the two tables, field1, field2, and field3. When there are matching rows in the two tables (table1.field1/2/3 = table2.field1/2/3)
    I want to replace table1.field4 with table2.field4 and replace table1.field5 with table2.field5.
    I have worked with the query but have come up with goobly goop. I would appreciate any help. Thanks.

    If your field1, field2, and field3 combinations in these tables are unique, you
    can do a join on them.
    Select t1.field4, t2.field4 , t1.field5, t2.field5
    from table1 t1 inner join table2 t2 on t1.field1 =t2.field1 and t1.field2=t2.field2 AND t1.field3=t2.field3
    --You can update your table1 with following code:
    Merge table1 t1
    using table2 t2 on
    on t1.field1 =t2.field1 and t1.field2=t2.field2 AND t3.field3=t2.field3
    When matched then
    Update Set
    t1.field4= t2.field4
    ,t1.field5 = t2.field5 ;

  • How to find varience records in two tables

    Hi,
    i have two tables like emp1, emp2
    data in emp1:
    empname empid add
    alasta 10 ca
    mark 20 nw
    chews 30 jp
    data in emp2:
    empname empid add
    alasta 10 ca
    mark 20 nw
    chews 30 jp
    burgess 45 ngm
    if u observe the above two tables data you can easily identify there is repeated data/duplicated data in both tables.
    as there any way find out using comparison same records in two tables..........
    if i write a query then i can easily understand the same data is existed in two tables
    Thanks in advance....

    >
    as there any way find out using comparison same records in two tables..........
    >
    >
    if i write a query then i can easily understand the same data is existed in two tables
    >
    So, what is your problem now?
    You can join both the tables using a simple query and get the records.
    select e1.empname, e2. empname from emp1 e1, emp2 e2 where
    e1.empname = e2.empname and e1.empid = e2.empid

  • Finding unmatched records between two tables

    Hi,
    Suppose there are two tables - A and B and they have a couple of common key fields.  I  want to select those records from A which do not have a matching record in B. What will be most efficient way to do that?
    Thank you.

    Hey,
    Look at this link .......... i think this may help you a bit to achieve ur requirement
    http://help.sap.com/saphelp_470/helpdata/en/fc/eb35f8358411d1829f0000e829fbfe/content.htm
    Comparing Internal Tables: -
    Internal tables can be compared with the operands that are used to compare other data objects. The most important criteria for comparing the internal table are the number of lines they contain. The larger the number of lines, the larger it is for comparisons. If the both the internal tables have same number of lines, then they are compared line by line. The operands used for comparisons are LE, LT, GE, GT, EQ, NE.
    Except for EQ, the comparison stops at the first pair of components that identifies the condition false.

  • How do I extract matching records from two tables?

    I'm trying to extract client records from our client database to put together a very targeted email campaign. As an example, the result I want is a list of names and email addresses of those clients who have a record in our system, have not had a visit in our clinic in the last year, and live within 200 miles of our clinic. I can capture the first two criteria in one extract, and the second in another. I can then import those as tab delimited data into Numbers. What I then need to do is create a third table that represents ONLY those records that exist in both tables.
    Can someone tell me if this is possible and if so, how to do it?
    I'd be very appreciative of any help, thank you.

    conejo61 wrote:
    I can then import those as tab delimited data into Numbers. What I then need to do is create a third table that represents ONLY those records that exist in both tables.
    You can create a column that generates a serial marker on the table from which you want to transfer the data, making all the names that also appear on the other table. Not that the formula will mark only exact matches.
    Here's a short example:
    Table 1 on the left, is one of the two tables imported from the tab delimited data files. It has other data, but only the names in column A are used to identify records appearing on both tables.
    Table 2 on the right, contains the names and other data (represented by the email addresses in column B), to be transferred to the third table.
    Column C of this table contains the formula that counts off the rows containing names appearing on both tables. The version below is in C2, and is filled down to the end of the column.
    =IF(COUNTIF(Table 1 :: $B,A2)>0,MAX($C$1:C1)+1,"")
    Note that jane Doe is not counted (or transferred) as her name is recorded differently on the two tables.
    Table 3 is the results table. It contains one formula for each column to be transferred from Table 2 to Table 3.
    A2:   =IF((ROW()-1>MAX(Table 2 :: $C)),"",LOOKUP(ROW()-1,Table 2 :: $C,Table 2 :: $A))
    B2:   =IF((ROW()-1>MAX(Table 2 :: $C)),"",LOOKUP(ROW()-1,Table 2 :: $C,Table 2 :: $B))
    Regards,
    Barry
    EDIT: I was working in Numbers '09 when developing this, and got the following warning when I saved a copy as an iWork '08 document:
    Referencing row or column ranges that include header or footer cells isn't supported. The formula references were updated to exclude header and footer cells.
    The formulas in Table 2::C2 references C1. You may have to ensure that Table 2 does not include a header row.
    B.
    EDIT2: A check on the file in Numbers '08 showed no apparent change to the formula, and editing Jane Doe's name on table 1 resulted in her name being added to Table 3.
    Message was edited by: Barry

  • Matching records from two tables even when the ID of a person is null.

    I have a MySQL named Natural Systems which is a magazine account that it member's will both sell handmade items wherein the proceeds will go into the Natural Systems account and each member also give a weekly donation. All of the proceeds will help toward the printing of the subsequent volumes. both of the selling of handmade items and the weekly dues  happens during the same week or the same magazine volume number. I am trying to write a query that will pull both of the sales as well as the weekly dues payments weather they have paid or not.
    SELECT foiid, trim(concat(name.fname,' ',name.lname)) AS no_ns, nspayamt, DATE_FORMAT(nspaydate, '%m/%d/%Y') FROM name LEFT JOIN nspay ON nspayfoiid = foiid and volume  = '27' WHERE nspayfoiid is null AND  type = 'Registered' AND city = 'richmond' AND status = 'a' group by foiid ORDER BY no_ns
    This code will bring up the following names of those who did not pay weekly dues during volume 2:
    Christopher Gabb
    Michael banks
    Timothy Hardin
    However there is another table I wish to call the same Individuals who sold magazine during the same week of volume 27
    SELECT trim(CONCAT(name.fname,' ',name.lname))AS Sold,
    round(sum(fcnsales.salesamt)) as TOTAL_Mags_SOLD FROM name, fcnsales WHERE fcnsales.salesfoiid = name.foiid
    AND fcnsales.salesvolume '27'  GROUP BY name.foiid ORDER BY TOTAL_mags_SOLD desc;
    This above code produces the following results:
    Sold                             TOTAL_Mags_SOLD  
    Christopher Gabb        50
    Michael banks             36       
    Timothy Hardin           12
    I would like to combine the two queries to produce the following
    Sold                             TOTAL_Mags_SOLD   nspayamt
    Christopher Gabb          50                                none    
    Michael banks               36                                none
    Timothy Hardin            12                               none
    can anyone give me a direction to go to?

    If your field1, field2, and field3 combinations in these tables are unique, you
    can do a join on them.
    Select t1.field4, t2.field4 , t1.field5, t2.field5
    from table1 t1 inner join table2 t2 on t1.field1 =t2.field1 and t1.field2=t2.field2 AND t1.field3=t2.field3
    --You can update your table1 with following code:
    Merge table1 t1
    using table2 t2 on
    on t1.field1 =t2.field1 and t1.field2=t2.field2 AND t3.field3=t2.field3
    When matched then
    Update Set
    t1.field4= t2.field4
    ,t1.field5 = t2.field5 ;

  • Filemaker-how to match and flag records in two tables

    Hello,
    Tiger.8
    Filemaker Pro 8.5 Advanced
    I have two related tables in an FMP file.
    Table A has ~300 records, B ~2,000.
    Each record in A is unique for sure; I believe each one in B is unique.
    The goal is to determine whether any of the records in B match one in A based upon student ID.
    These IDs are like social security numbers; i.e., they are preassigned, unique 9-character values rather than ones that I'm assigning within FMP.
    Like SSNs, these IDs are unique. So within Table A, each ID is unique. Likewise, within B the IDs are unique.
    However, some IDs in A might also be in B. I need to find out programmatically which ones, if any, are in both tables.
    If a match exists, I need to flag a field in each table.
    I found and modified an AppleScript that someone else wrote.
    The original is as follows.
    tell application "FileMaker Pro"
    activate
    show every record of database "Original Database"
    show every record of database "Old Database"
    set field "Match" of document "Original Database" to ""
    set orgList to field "Full Name" of document "Original Database"
    set oldList to field "Full Name" of document "Old Database"
    set matchList to field "Match" of document "Original Database"
    repeat with i from 1 to (count of orgList)
    if oldList contains item i of orgList then
    set item i of matchList to "Match"
    end if
    end repeat
    set field "Match" of document "Original Database" to matchList
    show (every record of database "Original Database" whose cell "Match" = "Match")
    sort layout 2 of document "Original Database" by field "Last Name"
    show layout 2 of document "Old Database"
    end tell
    My revision is as follows.
    tell application "FileMaker Pro Advanced"
    activate
    show every record of table "A" of document "test"
    show every record of table "B" of document "test"
    set field "flag" of table "A" of document "test" to ""
    set field "flag" of table "B" of document "test" to ""
    set AList to field "ID" of table "A" of document "test"
    set BList to field "ID" of table "B" of document "test"
    set flagList to field "flag" of table "A" of document "test"
    repeat with i from 1 to (count of AList)
    if BList contains item i of AList then
    set item i of flagList to "flag"
    end if
    end repeat
    set field "flag" of table "A" of document "test" to flagList
    set field "flag" of table "B" of document "test" to flagList
    show (every record of database "test" whose cell "flag" = "flag")
    sort layout 1 of document "test" by field "ID"
    show layout 1 of document "test"
    end tell
    I sort of understand what each of the above commands does, but would appreciate answers to the following questions.
    1. How come a test record that I created in A and B that has bogus ID 1234 does not get flagged by the above revised script, but other records with valid matching IDs in both A and B do get flagged?
    2. How come all records in A that got flagged by the revised script do have counterparts in B, but only some of the ones in B that have a match in A got flagged?
    3. How do the commands "set Alist" and "set Blist" function?
    4. How do I fix the revised script so that it properly, correctly matches IDs between A and B, then properly, correctly flags the records in A and B that have matching IDs?
    Many thanks in advance and happy weekend!
    G4/1.25 MDD   Mac OS X (10.4.8)  

    >
    as there any way find out using comparison same records in two tables..........
    >
    >
    if i write a query then i can easily understand the same data is existed in two tables
    >
    So, what is your problem now?
    You can join both the tables using a simple query and get the records.
    select e1.empname, e2. empname from emp1 e1, emp2 e2 where
    e1.empname = e2.empname and e1.empid = e2.empid

  • How to Find Two Matching Columns in Two Table(to create a JOIN)

    Hi Guys!
    I just wondering if it is possible to have a SQL query to find two matches columns in two table to create any type of joins?!
    Let's say I have two tables CUSTOMER + INVOICE is it possible to run a SQL query to find two join able columns without scanning the tables visually?!
    Best Regards,

    Well, there's the Data Dictionary you could query for foreign key columns/constraints.
    http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:661009003696

  • How to find unmatched records in huge tables

    Hi,
    I want to find out the faster approach, to find records in two tables which do not match.
    To make it clear, lets say we have two tables.
    table_1(col1 number, col2 varchar2(20), ....other columns)
    and
    table_2(col3 number, col4 varchar2(20), ....other columns)
    col1, col2 from table_1 corresponds to col3, col4 of table_2.
    If a record in table_1 does not exists in table_2 or if one of the column has different value, I want to find all such records.
    Being Oracle developer, I can easily find that using "outer join" or using "exists".
    But want to find out the fastest way, as those table has millions of records.
    Maybe using segment or something like that...
    Any suggestions?

    If "the" fastes way would exist oracle would not implement the others.
    You will have to test it. The easiest way is to use set autotrace on in sqlplus and compere the consistent gets of both statements (by the way: there is also the MINUS operator with also leads to an outer join but is easier to read).
    Dim

  • DELETE THE MATCHED RECORDS IN DB2 TABLE

    DELETE THE MATCHED RECORDS IN DB2 TABLE
    sql server table sqlserver_emp(c1,c2,c3,4)
    records:1 2 3 4
    DB2 table db2_emp(c1 key,c2,c3,c4)
    records:1 2 5 6 7 8
    Both tables having same structure
    Matched records : 1 2
    1.Delete the matched records in db2 table : 1 2 (without using truncate option for DB2 in Anywhere)
    2.Finally Load all records in sql server(Because duplicate records in db2 already removed so no duplicates occured)
    3.NEED final OUTPUT AS:db2 table: 5 6 7 8 1 2 3 4
    Note:
    1.DB2 truncate doesn't used
    2.STARING AREA: ORACLE
    3.SQLSERVER AND DB2 CLOSED ENVIRONMENT DOESN'T USED AS STAGING AREA.
    HOW MANY INTERFACES AND PROCEDURES SHOULD BE CREATE? WHAT ARE THEY?
    HOW TO REACH MY REQUIREMNT?
    Answer provider is more appricatable.
    Thanks in advance.
    Edited by: krishna on Nov 9, 2011 8:40 PM

    1st option
    See in this scenario you can use two interface and one procedure step.
    1st interface-->poricedure-->2nd interface
    1st interface will bring data to oracle staging-->then procedure will delete matched record from target using keys--->3rd interface will simple insert the the data in oracle staging.
    2nd option
    Insted of deleting the target matched records, you just update target matched record with latest records in your staging oracle.You can use IKM Merge
    Thanks

  • Need to find the Difference between two table

    Hello ,
    I have stucked in program as below scenario:-
    I have two tables of huge data of same structure in a same schema.I need to find the difference exact values in tables.
    By using MINUS we can find the difference between two table ,i need to find the what exact difference in the there values with colunm and value at that column.
    Example TableA
    Col1 col2 col3 col4 col5.... col50
    10 ABC 2001 EE 444 TT
    40 XYZ 3002 RR 445 TT3
    80 DEF 6005 YY 446 YY8
    TableB
    Col1 col2 col3 col4 col5.... col50
    10 ABC 2001 EE 444 TT
    40 XYZ 3002 RR 445 TT3
    81 DEF 6005 Yu 447 YY8
    I need to the out put like this :-
    The Diffence between two table is
    TableA.COL1=80 TableB.Col1=81, Different
    TableA.Col4=YY TableB.col4=Yu,Different
    TableA.Col5=446TableB.col5=447,Different
    Please suggest me to write the pl/sql program for the same
    thanx in advance
    KK

    Thanx friends for all your efforts
    I have a sample code for the same,this will compare the two tables for single row in each table .
    what r the modification needed for the multiple rows of values in the two tables??
    Please suggest!!
    CREATE OR REPLACE PROCEDURE test_compare
    IS
    TYPE t_col
    IS
    TABLE OF VARCHAR2 (30)
    INDEX BY PLS_INTEGER;
    l_col t_col;
    j NUMBER := 0;
    l_sql VARCHAR2 (2000);
    col1 VARCHAR2 (30);
    col2 VARCHAR2 (30);
    val1 NUMBER;
    val2 NUMBER;
    status VARCHAR2 (30);
    CURSOR c1
    IS
    SELECT column_id, column_name
    FROM all_tab_columns
    WHERE table_name = 'TEST1';
    BEGIN
    FOR i IN c1
    LOOP
    j := j + 1;
    l_col (j) := i.column_name;
    END LOOP;
    FOR k IN 1 .. j
    LOOP
    l_sql :=
    'SELECT '
    || ''''
    || l_col (k)
    || ''''
    || ', '
    || 'TEST2.'
    || l_col (k)
    || ', '
    || ''''
    || l_col (k)
    || ''''
    || ', '
    || 'TEST1.'
    || l_col (k )
    || ', '
    || 'DECODE(TEST2.'
    || l_col (k)
    || ' -TEST1.'
    || l_col (k)
    || ', 0, ''NO CHANGE'', ''CHANGED'') FROM TEST2, TEST1';
    EXECUTE IMMEDIATE l_sql INTO col1, val1,col2, val2, status;
    IF status = 'CHANGED'
    THEN
    DBMS_OUTPUT.put_line( 'TEST2.'
    || col1
    || '='
    || val1
    || ', TEST1.'
    || col2
    || '='
    || val2
    || ', '
    || status);
    END IF;
    END LOOP;
    EXCEPTION
    WHEN OTHERS
    THEN
    DBMS_OUTPUT.put_line ('Error:- ' || SQLERRM);
    END;
    /

  • Inserting records across multiple tables

    I'm still pretty new to working with databases, but have been
    fine using DW to use forms to add, edit and delete records from a
    flat table.
    I'm less sure about updating records across multiple tables,
    for example in a one to many rleationship with a look up table, eg
    If I have three tables
    Companies :
    CompanyID (INT, auto increment)
    Company
    Address
    etc
    Contacts :
    ContactID (INT, auto increment)
    FirstName
    LastName
    etc
    CompanyContacts :
    CompanyID (INT)
    ContactID (INT)
    It's straightforward enough to create pages to insert new
    records into the Companies or Contacts tables, but how do I go
    about populating the CompanyContacts table when I add a new record
    in the Contacts table, so that it becomes 'attached' to a
    particular 'Company'?
    If that makes sense.
    Iain

    I'm still pretty new to working with databases, but have been
    fine using DW to use forms to add, edit and delete records from a
    flat table.
    I'm less sure about updating records across multiple tables,
    for example in a one to many rleationship with a look up table, eg
    If I have three tables
    Companies :
    CompanyID (INT, auto increment)
    Company
    Address
    etc
    Contacts :
    ContactID (INT, auto increment)
    FirstName
    LastName
    etc
    CompanyContacts :
    CompanyID (INT)
    ContactID (INT)
    It's straightforward enough to create pages to insert new
    records into the Companies or Contacts tables, but how do I go
    about populating the CompanyContacts table when I add a new record
    in the Contacts table, so that it becomes 'attached' to a
    particular 'Company'?
    If that makes sense.
    Iain

  • Insert Matching Records from Lookup Table to Main Table

    First off, I want to say many thanks for all the help that I've been provided on here with my other posts. I really feel as though my SQL knowledge is much better than it was even a few short weeks ago, largely in part to this forum.
    I ran into a snag, which I'm hoping someone can provide me some guidance on. I have 2 tables an import table and a lookup table. What I need to have happen is anytime there are matches between the "Types" in the 2 tables, I need a single instance
    of the "Type" and all corresponding fields from the lookup table appended to the import table. There will only be a single instance of each type in the "Lookup" table. Below is an example of how the data might look and the results that
    I would need appended.
    tblLookup
    Type Name Address City
    A Dummy1 DummyAddress No City
    B Dummy2 DummyAddress No City
    C Dummy3 DummyAddress No City
    tblImport
    Type Name Address City
    A John Maple Miami
    A Mary Main Chicago
    A Ben Pacific Eugene
    B Frank Dove Boston
    Data that would be appended to tblImport
    Type Name Address City
    A Dummy1 DummyAddress No City
    B Dummy2 DummyAddress No City
    As you can see only a single instance will be inserted even though there may be multiple instances in the import table. This is the part that I'm struggling on. Any assistance would be appreciated.

    I'm not really sure how else to explain it. With my example, the join would be on "Type" As you can see, there are 2 matching records between the tables (A and B). I would need a single instance of A and B to be inserted into the import table. 
    Below is a SQL statement, which I guess is what you're asking for but it will not do what I need it to do. With the example that I have below, it would insert multiple instances of type "A" into the import table.
    INSERT INTO tblImport (Type, Name, Address, City)
    Select tblLookup.Type, tblLookup.Name,
    tblLookup.Address, tblLookup.City)
    From tblLookup
    Join tblImport on tblLookup.Type = tblImport.Type

  • Records from two tables in different DB's

    I have two tables (in different DB's) that I need to build
    results from. I want to return results from all records in table A
    and when a matching record in table B is found, retrieve the value
    of a sqlbit field. The catch is that there may or may not be any
    records in Table B that match Table A, and there may be more than
    one record in Table B, but always only one record for Table A.
    Table A is [Order] (yes I know, a reserved word - not my
    design) and Table B is tblOrderFollowup - here is my current SQL
    statement:
    ****************BEGIN SQL*************************
    SELECT o.StoreID AS xStore
    , o.Time AS dCreated
    , o.ID AS xPurchase
    , c.FirstName AS sFirstName
    , c.LastName AS sLastName
    , c.Company AS sCompany
    , c.ID AS xCustomer
    , o.Type AS xType
    , o.Closed AS bClosed
    , o.Tax AS nTax
    , fu.OrderID AS xFUOrder
    , fu.bComplete AS xFUComplete
    FROM [Order] o
    INNER JOIN Customer c
    ON o.CustomerID = c.ID
    AND o.StoreID = c.StoreID
    LEFT OUTER JOIN db2.dbo.tblOrderFollowup fu
    ON o.ID = fu.OrderID
    AND o.StoreID = fu.StoreID
    WHERE o.StoreID IN ( #allowableStoreIDs# )
    AND o.StoreID = <cfqueryparam cfsqltype="cf_sql_integer"
    value="#FORM.StoreID#" />
    AND o.Time >= <cfqueryparam
    cfsqltype="cf_sql_timestamp"
    value="#CreateODBCDateTime(StartOfDay(FORM.dFrom))#" />
    AND o.Time <= <cfqueryparam
    cfsqltype="cf_sql_timestamp"
    value="#CreateODBCDateTime(EndOfDay(FORM.dTo))#" />
    AND o.Closed = <cfqueryparam cfsqltype="cf_sql_bit"
    value="1" />
    ORDER BY o.Time DESC
    ***********END SQL**********************
    The problem here is that when I have more than 1 record per
    OrderID/StoreID pair in Table B, I get more than one result - I
    only want one. I am guessing I need a sub query, but don't know how
    to go about doing that at all ...

    You say you want only one result. Do you know which one you
    want?
    By the way, your query is probably not giving you the right
    answer. My experience is that if you do this:
    select stuff
    from table1 left join table2 on something
    where table2.somefield = somevalue
    you get the wrong answer. What I do instead is,
    select stuff
    from table1 left join
    (select stuff from table2
    where whatever) need_an_alias on somthing

  • SELECTing records from two tables. Set Operators, CASE, DECODE, ...

    Hi all,
    I have two tables:
    CRETE TABLE T1 (T1_COL1 NUMBER, T1_COL2 NUMBER)
    CRETE TABLE T2 (T2_COL1 NUMBER, T2_COL2 NUMBER)
    T1 may or may not have records. T2 always has records. There are two scenarios.
    Scenario 1:
    =======
    SELECT * FROM T1 returns five rows, and SELECT * FROM T2 returns 10 rows.
    Now I need the five rows from T1.
    Scenario 2:
    =======
    SELECT * FROM T1 returns zero rows, and SELECT * FROM T2 returns 10 rows.
    Now I need the 10 rows from T2.
    In other words, if records present in T1, I need them all. If not, I need records from T2.
    There are no common columns (for joins).
    Now need a single query to achive this. I tried set operators, CASE and DECODE. But I'm unable to solve it.
    Please help. Thanks in advance.

    Iniyavan wrote:
    Yes, Justin. I'm sure that this is the way it's modelled. I also find it's tough and odd.Are you sure it's the right way for that data to be modeled? I understand that's the way it is being modeled, but a data model change may be the best option.
    Is there any other way, which is simpler, without using RANK?That's the simplest option I can think of. You could also do something like
    SELECT t1_col1, t1_col2
      FROM (
        SELECT t1_col1, t1_col2, rownum rn
          FROM (
            SELECT t1_col1, t1_col2
              FROM (
                SELECT t1_col1, t1_col2, 1 tbl
                  FROM t1
                UNION ALL
                SELECT t2_col2, t2_col2, 2 tbl
                  FROM t2       
             ORDER BY tbl
    WHERE rn = 1I'm not sure that's any simpler...
    Justin

Maybe you are looking for