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

Similar Messages

  • 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

  • Help with listing records from two tables

    Hi: I have two tables joined by the first field. The field is primary key in first table. Need help listing records from both tables with each record one line/record.
    create table EVENTS (
    event_key varchar2(64) primary key,
    event_description varchar2(64),
    create_time int
    create table EVENT_UPDATES (
    event_key varchar2(64) NOT NULL ,
    update_description varchar2(64),
    update_time int
    insert into EVENTS values('Event1', 'This is event1', 1);
    insert into EVENT_UPDATES values('Event1', 'Ticket created', 3);
    insert into EVENT_UPDATES values('Event1', 'Event cleared', 10);
    insert into EVENTS values('Event2', 'This is event2', 4);
    insert into EVENT_UPDATES values('Event2', 'Ticket created', 6);
    insert into EVENT_UPDATES values('Event2', 'Event cleared', 8);I want to print each record in EVENTS table as one line and corresponding records in EVENT_UPDATES as one line/record like this
    Event1   1     This is event1
                3     Ticket created
                10   Event cleared
    Event2   4     This is event2
                6     Ticket created
                8     Event clearedTIA
    Ravi

    select  case weight
              when 1 then event_key
            end key,
            time_val,
            description
      from  (
              select  event_key,
                      create_time time_val,
                      event_description description,
                      1 weight
                from  events
             union all
              select  event_key,
                      update_time,
                      update_description,
                      2 weight
                from  event_updates
      order by event_key,
               weight
    KEY          TIME_VAL DESCRIPTION
    Event1              1 This is event1
                        3 Ticket created
                       10 Event cleared
    Event2              4 This is event2
                        6 Ticket created
                        8 Event cleared
    6 rows selected.
    SQL> SY.

  • 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

  • Can i select Records from two tables into an itab.

    Hi,
        Suppose that i have two tables spfli and sflight.
        Now i want the records from both the tables into a single internal table.
        If so please let me know how this can be done.
        itab should consist of all the columns in sflight and spfli.
    Regards,
    Sai

    Sai,
    Yes u can do it using JOINs.
    Use this link for ref
    http://www.sap-img.com/abap/inner-joins.htm
    INNER JOIN results are an intersection of the tables being joined where in only if both the tables havethe data the result is pused onto the result set.
    WHERE as in LEFT OUTR JOIN you can push the data of the LEF T table on the resultset even when the join condition is not met.
    The use is that you wantto have all the data that is there in left table and also the right table if the join condition is a success then right table fileds will have data else they are initial.
    OUTJOIN's are used in MAINTENANNCE VIEWS, HELP VIEWS .
    INNER JOINS are used DATABSE VIEWS.
    Inner Join:-
    SELECT pcarrid pconnid ffldate bbookid
    INTO CORRESPONDING FIELDS OF TABLE itab
    FROM ( ( spfli AS p
    INNER JOIN sflight AS f ON pcarrid = fcarrid AND
    pconnid = fconnid )
    INNER JOIN sbook AS b ON bcarrid = fcarrid AND
    bconnid = fconnid AND
    bfldate = ffldate )
    WHERE p~cityfrom = 'FRANKFURT' AND
    p~cityto = 'NEW YORK' AND
    fseatsmax > fseatsocc.
    Left Outer Join
    SELECT scarrid scarrname p~connid
    INTO CORRESPONDING FIELDS OF TABLE itab
    FROM scarr AS s
    LEFT OUTER JOIN spfli AS p ON scarrid = pcarrid AND
    p~cityfrom = 'FRANKFURT'.
    Reward if helpful,
    Karthik

  • Delete records from two tables

    Hi All,
    I have two tables (tableA and tableB) and i have four combination primary key.
    How can I delete the records from tableA that are not in tableB?
    In my sample below I need to delete the records from year 2013.
    I have a loop to insert the new ones.
    Thanks
    Johnny
    create table tableA(
    keyA_id number,
    keyB_id number,
    keyC_id number,
    keyD_id number,
    amount  number,
    CONSTRAINT "PK_TABLEA" PRIMARY KEY (keyA_id, keyB_id, keyC_id, keyD_id)
    insert into tableA(keyA_id,keyB_id,keyC_id,keyD_id,amount)
    values(1,1,1,2011,10);
    insert into tableA(keyA_id,keyB_id,keyC_id,keyD_id,amount)
    values(1,1,1,2012,40);
    insert into tableA(keyA_id,keyB_id,keyC_id,keyD_id,amount)
    values(1,1,1,2013,20);
    insert into tableA(keyA_id,keyB_id,keyC_id,keyD_id,amount)
    values(1,1,2,2011,10);
    insert into tableA(keyA_id,keyB_id,keyC_id,keyD_id,amount)
    values(1,1,2,2012,30);
    insert into tableA(keyA_id,keyB_id,keyC_id,keyD_id,amount)
    values(1,1,2,2013,20);
    create table tableB(
    keyA_id number,
    keyB_id number,
    keyC_id number,
    keyD_id number,
    amount  number,
    CONSTRAINT "PK_TABLEB" PRIMARY KEY (keyA_id, keyB_id, keyC_id, keyD_id)
    insert into tableB(keyA_id,keyB_id,keyC_id,keyD_id,amount)
    values(1,1,1,2011,10);
    insert into tableB(keyA_id,keyB_id,keyC_id,keyD_id,amount)
    values(1,1,1,2012,40);
    insert into tableB(keyA_id,keyB_id,keyC_id,keyD_id,amount)
    values(1,1,1,2014,40);
    insert into tableB(keyA_id,keyB_id,keyC_id,keyD_id,amount)
    values(1,1,2,2011,10);
    insert into tableB(keyA_id,keyB_id,keyC_id,keyD_id,amount)
    values(1,1,2,2012,30);
    insert into tableB(keyA_id,keyB_id,keyC_id,keyD_id,amount)
    values(1,1,2,2014,30);

    r you trying to do something like below ?
    Delete from TableA A
    where not exists (select 1 from TableB B where
    A.keyA_id=B.keyA_id and
    A.keyB_id=B.keyB_id and
    A.keyC_id=B.keyC_id and
    A.keyD_id=B.keyD_id)

  • 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

  • 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 access records from two tables which have no relation

    Hi,
    I trying to generate a report where i need to print the company details at the top of the page and invoice details of that down the page.There is no relation between these two tables.I am not able to write two queries for one report.Pls some one assist me in getting this thing done.
    Regards,
    Tulacenath.

    Hi Tulacenath
    So your invoice table does not have a reference to the customers (companys) that the invoices belong to?
    Tim

  • 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 ;

  • Display records from two tables in a Repeat Region

    I have 2 tables tblNames and tblResults
    tblNames
    NamesID PK
    FirstName
    LastName
    Address1
    Addrees2
    tblResults
    ResultsID PK
    NamesID FK
    ResultsInfo
    ResultsDate
    Results table has a foreign key NamesID containing primary
    key of Names table.
    I am trying to show a repeat region of results, with a column
    showing LastName of tblNames depending on the ResultsID
    I have 2 recordsets, rsNames and rsResults but I'm having
    trouble showing relevant LastName.

    Create a new recodset or modify one of the old ones with this
    sql:
    SELECT LastName, ResultsInfo, ResultsDate
    FROM tblResults INNER JOIN tblNames USING (NamesID)
    ORDER BY LastName
    This will produce a list of LastNames and associated info
    Dave Buchholz
    I-CRE8
    www.i-cre8.co.uk
    Skype ID: I-CRE8
    "Swn-Y-Mor" <[email protected]> wrote in
    message
    news:f1ndo2$cp9$[email protected]..
    >I have 2 tables tblNames and tblResults
    >
    > tblNames
    > --------
    > NamesID PK
    > FirstName
    > LastName
    > Address1
    > Addrees2
    >
    > tblResults
    > ----------
    > ResultsID PK
    > NamesID FK
    > ResultsInfo
    > ResultsDate
    >
    > Results table has a foreign key NamesID containing
    primary key of Names
    > table.
    >
    > I am trying to show a repeat region of results, with a
    column showing
    > LastName
    > of tblNames depending on the ResultsID
    > I have 2 recordsets, rsNames and rsResults but I'm
    having trouble showing
    > relevant LastName.
    >

  • How to handle an update of the same record from two different user JSP

    how do you handle an update of the same record from two different users in JSP... if one person updates the record from one drop downs should be updated as well.. is the possible.

    I'm not sure whether I understand your question. If one user changes a record then you would like other users to see those changes. Normally those changes will be visible to other users when they refresh their browser, after the first user has committed changes to the record. I you want to be sure that the same row isn't updated at the same time by to different user, you need to configure some locking (pessimistic locking) - this depends of what technology you use. In EJB3 pessimistic locking is performed by adding a version to every entity object. If you are using ADF, the framework is able to handle either pessimistic or even optimistic locking.
    If you want the changed row to be updated in other users browsers without any user interaction by these users, you should take a look at Reverse Ajax (ex. DWR http://ajaxian.com/archives/reverse-ajax-with-dwr) - or Ajax in general. But you will never get a realtime solution, where changes is visible in other users browsers right after each record update.

  • How to handle an update of the same record from two different user  in JSP

    how to handle an update of the same record from two different user
    how do you handle an update of the same record from two different users in JSP... if one person updates the record from one drop downs should be updated as well.. is the possible.

    Usually, if two users try to update the same row at the same time, you want the first to commit to succeed, and when the second commits, they should fail with the error that the row was being concurrently updated. They you may want to show them the new row values and give them the opportunity to merge their values with the new row values...
    How can you achieve this? Google optimistic locking.

  • How can i fetch records from 3 tables in a single query  without using join

    Hi.
    Can any body please tell me <b>How can i fetch records from 3 tables with a single query  without using joins</b>
    Thanx
    prabhudutta

    Hi Prabgudutta,
    We can fetch the data by using the views concept.
    Go throuth this info we can know the how to create view and same like database table only we can fetch the data.
    Views conatin the data at runtime only.
    Four different view types are supported. These differ in the
    way in which the view is implemented and in the methods
    permitted for accessing the view data.
    Database views are implemented with an equivalent view on
    the database.
    Projection views are used to hide fields of a table (only
    projection).
    Help views can be used as selection method in search helps.
    Maintenance views permit you to maintain the data
    distributed
    on several tables for one application object at one time.
    step by step creation of Maintenance view:
    With the help of the table maintenance generator, you are able to maintain the ENTRIES of the table in SM30 transaction.
    It can be set in transaction SE11 - Tools - Table maintenance generator.
    Table maintanance Generator is used to manually input values using transaction sm30
    follow below steps
    1) go to se11 check table maintanance check box under attributes tab
    2) utilities-table maintanance Generator-> create function group and assign it under
    function group input box. Also assign authorization group default &NC& .
    3) select standard recording routine radio in table table mainitainence generator to move table
    contents to quality and production by assigning it to request.
    4) select maintaience type as single step.
    5) maintainence screen as system generated numbers this dialog box appears when you click on create button
    6) save and activate table
    One step, two step in Table Maintenance Generator
    Single step: Only overview screen is created i.e. the Table Maintenance Program will have only one screen where you can add, delete or edit records.
    Two step: Two screens namely the overview screen and Single screen are created. The user can see the key fields in the first screen and can further go on to edit further details.
    SM30 is used for table maintenance(addition or deletion of records),
    For all the tables in SE11 for which Table maintenance is selected , they can be maintained in SM30
    Sm30 is used to maintain the table ,i.e to delete ,insert or modify the field values and all..
    It creates the maintenance screen for u for the aprticular table as the maintenance is not allowed for the table..
    In the SE11 delivery and maintenance tab, keep the maintenance allowed..
    Then come to the SM30 and then enter the table name and press maintain..,
    Give the authorization group if necessary and give the function group and then select maintenance type as one step and give the screen numbers as system specified..
    Then create,,,
    Then u will able to see the maintenance view for the table in which u can able to insert and delete the table values...
    We use SM30 transaction for entering values into any DB table.
    First we create a table in SE11 and create the table maintenance generator for that Table using (utilities-> table maintenance generator) and create it.
    Then it will create a View.
    After that from SM30, enter the table name and Maintain, create new entries, change the existing entries for that table.
    Hope this resolves your query.
    Reward all the helpful answers.
    Rgds,
    P.Naganjana Reddy

  • 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

Maybe you are looking for