Query across two tables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anonymous
    Banned
    New Member
    • Sep 2005
    • 99

    Query across two tables

    Please help me with this query..
    I have two tables..One is customer details table and another transaction details table.
    The customer details table has all the details of internal customers..
    like following
    Cust_id Accnt_id
    1 001
    2 002
    and so on..
    The transaction details table has details like
    TxnId Acnt_id Type benAcnt DrAcntId
    12 001 dep 001 235
    13 001 dep 001 002
    14 001 with 456 001
    15 001 with 003 845
    And so on..
    What i want is the list of all account ids that 001 has transacted with irrespective of type..Here 001,002,003 are internal customers while 235,456,845 are external customers whose details are not available in customer details table..So i want the list of such external customers transacted with customer 001. Some thing like following,
    Acnt_id ExternalAcnt_id
    001 235
    001 456
    001 845 . Please help in forming query for this..
  • azimmer
    Recognized Expert New Member
    • Jul 2007
    • 200

    #2
    Originally posted by anonymous
    Please help me with this query..
    I have two tables..One is customer details table and another transaction details table.
    The customer details table has all the details of internal customers..
    like following
    Cust_id Accnt_id
    1 001
    2 002
    and so on..
    The transaction details table has details like
    TxnId Acnt_id Type benAcnt DrAcntId
    12 001 dep 001 235
    13 001 dep 001 002
    14 001 with 456 001
    15 001 with 003 845
    And so on..
    What i want is the list of all account ids that 001 has transacted with irrespective of type..Here 001,002,003 are internal customers while 235,456,845 are external customers whose details are not available in customer details table..So i want the list of such external customers transacted with customer 001. Some thing like following,
    Acnt_id ExternalAcnt_id
    001 235
    001 456
    001 845 . Please help in forming query for this..
    Code:
    select X.*
    from (
        select Acnt_id, benAcnt as ExternalAcnt_id
        from TRANSACTIONSTABLE
        where benAcnt>='200'
    
        union 
    
       select Acnt_id, DrAcntId as ExternalAcnt_id
       from TRANSACTIONSTABLE
       where DrAcntId>='200') as X
    where Acnt_id = '001'

    Comment

    • anonymous
      Banned
      New Member
      • Sep 2005
      • 99

      #3
      Originally posted by azimmer
      Code:
      select X.*
      from (
          select Acnt_id, benAcnt as ExternalAcnt_id
          from TRANSACTIONSTABLE
          where benAcnt>='200'
      
          union 
      
         select Acnt_id, DrAcntId as ExternalAcnt_id
         from TRANSACTIONSTABLE
         where DrAcntId>='200') as X
      where Acnt_id = '001'

      Its not always that external account id is greater than 200..It can have any id..only thing is that the external ids are not present in customer details table.

      Comment

      • azimmer
        Recognized Expert New Member
        • Jul 2007
        • 200

        #4
        Originally posted by anonymous
        Its not always that external account id is greater than 200..It can have any id..only thing is that the external ids are not present in customer details table.
        OK, here it goes:
        Code:
        select X.*
        from (
            select Acnt_id, benAcnt as ExternalAcnt_id
            from TRANSACTIONSTABLE
            where benAcnt not in (select Acnt_id from CUSTOMERS)
        
            union 
        
            select Acnt_id, DrAcntId as ExternalAcnt_id
            from TRANSACTIONSTABLE
            where DrAcntId not in (select Acnt_id from CUSTOMERS)) as X
        where Acnt_id = '001'

        Comment

        Working...