Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

query to get all rows in join from 2 tables

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
View previous topic :: :: View next topic  
Author Message
Krishnadeva Reddy

New User


Joined: 14 Nov 2007
Posts: 36
Location: Chennai

PostPosted: Tue Nov 15, 2011 8:47 am    Post subject: query to get all rows in join from 2 tables
Reply with quote

Hi,

I'm trying to get all rows from two tables by joining.
When there is no corresponding row found in TABLEB, I'm trying to get blanks in the output but my query is not giving desired results

Please advise.

TABLEA - sample data and this table has more rows than this

Code:

---------+---------+---------+---------+---------+---------+-----
B_OFF#   ID_USER  SVC_FRM_DT  SVC_THRU_DT  P_ID   
---------+---------+---------+---------+---------+---------+------
300000610  761001  2011-06-01     2011-06-01             0
300000620  761002  2011-06-01     2011-06-01     351001   
310544110  760996  2010-10-19     2010-10-19     352001   
310544220  760997  2010-10-19     2010-10-19     451003   
310544330  760998  2010-10-19     2010-10-19             0



TABLEB - sample data and this table has more rows than this

Code:

---------+---------+---------+---------
   P_ID   CHK_AMT  ISS_DTE     CHK_#
---------+---------+---------+---------
  351001    50.00  2011-06-01  A123-24
  352001    75.00  2011-06-02  A123-56
  451003    80.00  2010-10-19  A123-97


The output I'm getting from the below query

Code:

SELECT A.B_OFF#
           ,A.ID_USER
           ,B.CHK_#
           ,B.CHK_AMT
  FROM TABLEA.A, TABLEB.B
WHERE A.P_ID = B.P_ID
    AND ID_USER IN (761001,
                               761002,
                               760996,
                               760997,
                               760998)
     ;

---------+---------+---------+---------+-------
     B_OFF#      ID_USER  CHK_#      CHK_AMT
---------+---------+---------+---------+-------
  300000620       761002  A123-24    50.00
  310544110       760996  A123-56    75.00
  310544220       760997  A123-97    80.00


Can you please tell me to get all rows from 2 tables even if there is no match found in TABLEB?

The output i'm looking is

Code:

---------+---------+---------+---------+-------
     B_OFF#      ID_USER  CHK_#      CHK_AMT
---------+---------+---------+---------+-------
  300000610       761001 
  300000620       761002  A123-24    50.00
  310544110       760996  A123-56    75.00
  310544220       760997  A123-97    80.00
  310544330       760998 
Back to top
View user's profile Send private message

chandan.inst

Active User


Joined: 03 Nov 2005
Posts: 269
Location: Mumbai

PostPosted: Tue Nov 15, 2011 10:22 am    Post subject:
Reply with quote

Hi Krishna,
I belive you want to write all rows from Table A and when you find a match in Table B write amounts from table B in report else populate spaces for.

Here you need to use left outer join as below

Code:
SELECT A.B_OFF#
           ,A.ID_USER
           ,B.CHK_#
          , B.CHK_AMT
      FROM TABLEA.A
          LEFT OUTER JOINT
            TABLEB.B 
      ON A.P_ID = B.P_ID
      AND ID_USER IN (761001,
                               761002,
                               760996,
                               760997,
                               760998)
     ;


When B.CHK_# and B.CHK_AMT are null you need to populate Spaces to your corresponding report variables

Regards,
Chandan
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Join in SQL Query vickey_dw DB2 2 Tue Jan 17, 2017 12:15 am
This topic is locked: you cannot edit posts or make replies. MQ setup query mohitsaini Java & MQSeries 4 Sun Jan 08, 2017 1:53 am
No new posts DB2 query Using Recursion, Converting... smilewithashu2 DB2 1 Tue Jan 03, 2017 12:50 pm
No new posts SQL query to run through list of valu... Ni3-db2 DB2 14 Wed Dec 14, 2016 9:52 am
This topic is locked: you cannot edit posts or make replies. Db2 SQL query to convert rows into co... subratarec DB2 5 Thu Nov 17, 2016 4:51 pm


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us