IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

query to get all rows in join from 2 tables


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
Krishnadeva Reddy

New User


Joined: 14 Nov 2007
Posts: 37
Location: Chennai

PostPosted: Tue Nov 15, 2011 8:47 am
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: 275
Location: Mumbai

PostPosted: Tue Nov 15, 2011 10:22 am
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 View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts To get the count of rows for every 1 ... DB2 3
No new posts RC query -Time column CA Products 3
No new posts Exclude rows with > than x occurre... DFSORT/ICETOOL 6
No new posts Dynamically pass table name to a sele... DB2 2
No new posts Query on edit primary command CLIST & REXX 5
Search our Forums:

Back to Top