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

To join 2 tables and to join 3rd table with resultant


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

New User


Joined: 25 Nov 2021
Posts: 22
Location: India

PostPosted: Tue Jun 24, 2025 11:12 am
Reply with quote

Hi Team,

My reqt is as follows:

I have joined Table 1 and Table 2 based on Cust ID and State code and got 1 row as follows with Columns as State Code - Cust ID - Cust Name - Eff Date.

Code:
TN   123   CAR    2025-06-24 


I have Table 3 that has State code, Cust ID, Eff Date that has multiple rows

Code:
TN   123   2020-10-01
TN   123   2025-06-25
TN   123   2025-10-15


I will have to check the results of Table1 and 2 with Table 3 (it can have matching rows (State code - Cust ID) or rows may not match too) and we need to have resultant rows as

Code:
TN   123   CAR    2025-06-24
TN   123   CAR    2020-10-01 
TN   123   CAR    2025-06-25 
TN   123   CAR    2025-10-15


Could you please help me with the logic to achieve this. I thought of joining Table 1 ,2,3 but Table 3 has only minimal rows and it may or may not match with rows of Table 1/2. That means all rows fetched from ytable 1 and table 2 along with the Effective date from Table 3. If multiple rows are there with Eff date, we need to have that much rows as resultant.

Thanks
Chidane
Back to top
View user's profile Send private message
sergeyken

Senior Member


Joined: 29 Apr 2008
Posts: 2218
Location: USA

PostPosted: Tue Jun 24, 2025 5:25 pm
Reply with quote

From your post it is not clear: what exactly prevents you from joining (not "checking"!) your initial table with Table 3?
What did you try so far?
Where is any code sample you tried?
What are the results you are not satisfied with?

Etc, etc, etc...
Back to top
View user's profile Send private message
Chidane

New User


Joined: 25 Nov 2021
Posts: 22
Location: India

PostPosted: Tue Jun 24, 2025 5:53 pm
Reply with quote

Hi Sergeyken,

The Table 1 and table 2 has huge number of rows.
While Table 3 has minimal number of rows and not all rows of Table 1/2 is present in Table 3.
However we need all rows of table 1 and 2.

So for every row fetched from Table 1 and 2, if there are multiple rows in Table 3, I need to write to flat file as Multiple records.
Back to top
View user's profile Send private message
Chidane

New User


Joined: 25 Nov 2021
Posts: 22
Location: India

PostPosted: Tue Jun 24, 2025 5:54 pm
Reply with quote

I have tried Joining all 3 tables, however only matching rows of Table 1 and 2 and 3 are only fetched.

In this case, I need all matching rows of table 1 and 2 and then for every multiple rows in Table 3, w eneed to create separate record entry and writre it to flat file.
Back to top
View user's profile Send private message
sergeyken

Senior Member


Joined: 29 Apr 2008
Posts: 2218
Location: USA

PostPosted: Tue Jun 24, 2025 6:06 pm
Reply with quote

WHERE IS ANY SAMPLE OF THE USED CODE, AND EXACT COPY OF INPUT/OUTPUT DATA???

Without YOUR OWN samples of code you cannot expect others would give you a ready-to-copy-and-paste solution.



There is no need to start with "huge number of rows".
Run your initial tests with a limited table size: 2-3-5 rows in each of them, and present all your results here, - to receive any suggestion in response.
Back to top
View user's profile Send private message
sergeyken

Senior Member


Joined: 29 Apr 2008
Posts: 2218
Location: USA

PostPosted: Tue Jun 24, 2025 6:30 pm
Reply with quote

Hint:

I hope it is clear that TWO STEPS are required?

1) Join Table 1 and Table 2, producing Table X?
2) Join Table X and Table 3, producing final Table/Report?

Each step must operate with TWO TABLES. There is no need to operate with THREE TABLES simultaneously...
Back to top
View user's profile Send private message
sergeyken

Senior Member


Joined: 29 Apr 2008
Posts: 2218
Location: USA

PostPosted: Thu Jun 26, 2025 8:00 pm
Reply with quote

Chidane wrote:
I have tried Joining all 3 tables, however only matching rows of Table 1 and 2 and 3 are only fetched.


Looks like no more information and/or any tried code (hopefully, SQL query?) is expected here...

One more hint attempt.

In SQL, in order to join both matching and non-matching records from DB2 tables there are special constructions:
LEFT OUTER JOIN
RIGHT OUTER JOIN
FULL OUTER JOIN


Try to RTFM.
Back to top
View user's profile Send private message
Rohit Umarjikar

Global Moderator


Joined: 21 Sep 2010
Posts: 3098
Location: NYC,USA

PostPosted: Sat Jun 28, 2025 7:40 am
Reply with quote

Do table 1,2 inner join and table 3 left join .
In the where just say table3.state id is not null
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 Create a specific record/file based o... SYNCSORT 8
No new posts Extract ISPF table column headings CLIST & REXX 2
No new posts Unload and Load ISPF Table TSO/ISPF 4
No new posts ISPF Table to add a new column TSO/ISPF 1
No new posts Need to introduce Db table for every ... All Other Mainframe Topics 1
Search our Forums:


Back to Top