Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2004
    Posts
    14

    Unanswered: Can I use select inside a select??? If so how?

    The select that I want using is like this....
    Select (Select * from Databasename where Columnname = 'Value1' and Column2 is not null) from Databasename where Columnname <> 'Value'

    DB2 version 8 I think

    Thanks
    Last edited by Paingiver; 08-25-06 at 07:40.

  2. #2
    Join Date
    Feb 2004
    Posts
    14
    I want to get value that have some complex condition like if Columnname = 'Value1' then Column2 is not null is the bellow sql possible?

    Select * from Databasename where Columnname <> 'Value' and (if Columnname = 'Value1' then Column2 is not null)

    Thanks

  3. #3
    Join Date
    Dec 2005
    Posts
    273
    How about:


    Select *
    from Databasename
    where Columnname <> 'Value' and
    (Columnname = 'Value1' and Column2 is not null or Columnname ^= 'Value1' )

  4. #4
    Join Date
    Sep 2004
    Location
    Belgium
    Posts
    1,126
    Quote Originally Posted by umayer
    Select *
    from Databasename
    where Columnname <> 'Value' and
    (Columnname = 'Value1' and Column2 is not null or Columnname ^= 'Value1')
    Somewhat shorter:
    Code:
    Select * 
    from   Databasename 
    where  Columnname <> 'Value'
      and  (Column2 is not null or Columnname <> 'Value1')
    This is a general logic conversion rule:
    "if A then B" is equivalent with "(not A) or B".
    --_Peter Vanroose,
    __IBM Certified Database Administrator, DB2 9 for z/OS
    __IBM Certified Application Developer
    __ABIS Training and Consulting
    __http://www.abis.be/

  5. #5
    Join Date
    Feb 2004
    Posts
    14
    Thanks for the replys.

  6. #6
    Join Date
    Jan 2003
    Posts
    1,605
    Hi,

    if you would like to write select inside select you can write:
    Select from (Select * from Databasename where Columnname = 'Value1' and Column2 is not null) as temp where Columnname <> 'Value'

    Hope this helps,
    Grofaty

  7. #7
    Join Date
    Dec 2005
    Posts
    273
    Quote Originally Posted by grofaty
    Select from (Select * from Databasename where Columnname = 'Value1' and Column2 is not null) as temp where Columnname <> 'Value'
    I disagree.

    The correct nested table expression is:

    Select from (Select * from Databasename where Columnname ^= 'Value1' or Column2 is not null) as temp where Columnname <> 'Value'

Posting Permissions

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