Results 1 to 10 of 10
  1. #1
    Join Date
    Jun 2015
    Posts
    6

    Unanswered: Dynamic Column name in where clause

    Hello All
    I'm new to this forum ..not sure where to post this question.
    I have below requirement ..
    Need to select level number from table A and get required columns from table B based on the value from Table A

    Select emplid, level_number from table A ..example level is 2

    In table B we have different columns like this level1 , level2 , level3 ..

    now .select * from table B where level2 ='xxxx' ..
    pls help me out on this

  2. #2
    Join Date
    Jan 2003
    Posts
    4,292
    Provided Answers: 5
    There is not enough information here to give you an answer.

    Please expand on what you want (Give examples if that will help). Also give DB2 version and OS.

    Andy

  3. #3
    Join Date
    Jun 2015
    Posts
    6
    DB2 Version 8

    and Windows .

    Select a.level , b.managername from table a, table b
    where a.level = 2 (this number is dynamic) and b.level2_nuid ='1234'

    b.level2_nuid - this column name should be dynamic based on the level number from table a ..

  4. #4
    Join Date
    Jan 2003
    Posts
    4,292
    Provided Answers: 5
    First V8 went out of service over 6 years ago. You really should consider getting current.

    Still not enough information, although I suspect you need to use the CASE function in the where clause. I would suspect that this would cause the resulting SQL to perform badly.

    Andy

  5. #5
    Join Date
    May 2012
    Location
    Canberra, Australia
    Posts
    176
    Provided Answers: 6
    you could give the ddl for the two tables for a start. I hope you aren't running a production system on V8
    Andy

    "All parts should go together without forcing. You must remember that the parts you are reassembling were disassembled by you. Therefore, if you can’t get them together again, there must be a reason. By all means, do not use hammer.” — IBM maintenance manual, 1975 "

  6. #6
    Join Date
    Jun 2015
    Posts
    6
    Quote Originally Posted by ARWinner View Post
    First V8 went out of service over 6 years ago. You really should consider getting current.

    Still not enough information, although I suspect you need to use the CASE function in the where clause. I would suspect that this would cause the resulting SQL to perform badly.

    Andy
    Not sure about the DB2 version - might be latest version .. I tried CASE function but didn't work out ..

    Pls let me know the any sample query using CASE statement ..

    Rgds

  7. #7
    Join Date
    Apr 2008
    Location
    Iasi, Romania
    Posts
    561
    Provided Answers: 2
    Florin Aparaschivei
    DB2 9.7, 10.5 on Windows
    Iasi, Romania

  8. #8
    Join Date
    Jun 2015
    Posts
    6
    Thanks

    I don't have access to create Procedures / PL SQL etc ..

    I have to do this with in SQL on DB2 only ..might be two separate SQL's - Like in 1 SQL get the level of the selected employee and in other SQL based on that Level get the required column to join to get all the employees reporting to that employee

  9. #9
    Join Date
    May 2010
    Location
    India
    Posts
    84
    Provided Answers: 2
    Try this.. .

    Select a.level , b.managername from table a, table b
    where
    (
    ( a.level = 1 and b.level1_nuid ='1234' ) or
    ( a.level = 2 and b.level2_nuid ='1234' ) or
    ( a.level = 3 and b.level3_nuid ='1234' )
    )
    and a.emplid = '1234'

  10. #10
    Join Date
    Jun 2015
    Posts
    6
    Thanks will try

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •