If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Joining on concatenation of fields

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-17-08, 14:28
PKPChuck PKPChuck is offline
Registered User
 
Join Date: Sep 2006
Location: Columbus, OH
Posts: 64
Joining on concatenation of fields

AIX 5L
DB2 8.2

Table A looks like this

Column1 Column2 Column3
123141 1 1
123142 1 2

Table B looks like this

Column1 ....
12314111
12314212


The primary key of Table B is all three columns in TableA. So I'm trying to figure an easy way to join these two tables using this information.

Thanks,

Charlie
Reply With Quote
  #2 (permalink)  
Old 09-17-08, 14:43
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
SELECT ...
from tablea as a
inner join tableb as b on (b.column1 = a.column1||columns2||column3)
where ..

Andy
Reply With Quote
  #3 (permalink)  
Old 09-19-08, 08:48
laknar laknar is offline
Registered User
 
Join Date: Jul 2008
Posts: 80
try this

SELECT ...
from tablea as a
inner join tableb as b on (b.column1 = column1||columns2||column3)
where ..
Reply With Quote
  #4 (permalink)  
Old 09-20-08, 05:57
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by ARWinner
SELECT ...
from tablea as a
inner join tableb as b on (b.column1 = a.column1||column2||column3)
where ..
This *might* give incorrect results (viz. empty result table), depending on the datatypes of the columns; the above solution only works when a.column1 is either of datatype VARCHAR(n) or CHAR(6), and a.column2 is of datatype VARCHAR(n) or CHAR(1).
(Presuming that all data in your table is similar in width to what you example tells us ...)
Three alternative solutions (without the above restrictions):
Code:
SELECT ...
from a inner join b on b.column1 = rtrim(a.column1)||rtrim(column2)||column3
where ..
(Now, the restriction being that b.column1 does not have embedded blanks...)
Code:
SELECT ...
from a inner join b on
      substr(b.column1,1,6) = a.column1 AND
      substr(b.column1,7,1) =   column2 AND
      substr(b.column1,8,1) =   column3
where ..
Finally, you may consider adding three (redundant) columns to table b, containing those substr()s; this might give a much more performant query (depending, of course, on the sizes of the two tables).
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #5 (permalink)  
Old 09-25-08, 10:25
PKPChuck PKPChuck is offline
Registered User
 
Join Date: Sep 2006
Location: Columbus, OH
Posts: 64
Thanks for the response. I got the concatenation query to work. However, I found this function is time costly to the overall query. I think I might add a column or table and perform the concatenation at INSERT instead.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On