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 > Query Column Name is Value in another table

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-12-09, 16:14
phil72 phil72 is offline
Registered User
 
Join Date: Nov 2008
Posts: 41
Query Column Name is Value in another table

Hi
i have Table1
Columns Col1 ,col2
Values AA A1
BB A2
CC B3


Table 2
Columns A1, A2, B3, B4 ...
values x y z 1

I want to get result :

AA x
BB y
CC z

Select Tab1.col1, (select Tab1.col2 from Table2 Tab2) from Table1 Tab1

can Tab1.col2 evaluate to return value from Tab2?

Thanks in Advance
Reply With Quote
  #2 (permalink)  
Old 11-12-09, 16:54
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
You could use the CASE expression for that:
Code:
select
  t1.col1,
  case t1.col2 
    when 'A1' then t2.a1
    when 'A2' then t2.a2
    ...
   end
from table1 t1, table2 t2
Make sure the columns in table2 are cast to the same type, if they are not originally of the same type.
Reply With Quote
  #3 (permalink)  
Old 11-12-09, 17:02
phil72 phil72 is offline
Registered User
 
Join Date: Nov 2008
Posts: 41
Thanks Ivanov

The case state works , but the problem is that my vertical table Table1 has 23 rows and it can grow .
Reply With Quote
  #4 (permalink)  
Old 11-12-09, 17:16
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally Posted by phil72 View Post
the problem is that my vertical table Table1 has 23 rows and it can grow .
And why is that a problem?
Reply With Quote
  #5 (permalink)  
Old 11-12-09, 21:25
phil72 phil72 is offline
Registered User
 
Join Date: Nov 2008
Posts: 41
Yes , using case it becomes hardcoded statement, when new rows are added to the table1, then i will have to change query again, as i dont know how many rows will be added and wat values these rows will have.
Reply With Quote
  #6 (permalink)  
Old 11-13-09, 01:46
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
But, you knows column names of table 2.
So, you can include ELSE clause for the value of table 1 which doesn't match with column names of table 2.
Reply With Quote
  #7 (permalink)  
Old 11-13-09, 09:38
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally Posted by phil72 View Post
then i will have to change query again, as i dont know how many rows will be added and wat values these rows will have.
That's the price you pay for bad design.
Reply With Quote
  #8 (permalink)  
Old 11-13-09, 15:44
phil72 phil72 is offline
Registered User
 
Join Date: Nov 2008
Posts: 41
Thanks Ivanov

Any suggestions regarding my other thread

can we use Merge and With Together
Reply With Quote
  #9 (permalink)  
Old 11-13-09, 16:34
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
You have to use Dynamic SQL, that it.

Lenny
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