Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2010
    Posts
    40

    Unanswered: Creating one filed combining two other

    Hello,
    Is there any better way to merge distinct values of two fields of the same table filtered by the same constraint?
    See example:
    Code:
    SELECT DISTINCT col1 FROM tb1 WHERE col1='something' OR col2='else'
     UNION 
    SELECT DISTINCT col2 from tb1  WHERE col1='something' OR col2='else'
    Thanks
    Last edited by krontrex; 03-13-12 at 13:33. Reason: typing error

  2. #2
    Join Date
    Jan 2012
    Posts
    84
    Quote Originally Posted by krontrex View Post
    Code:
    SELECT DISTINCT col1 FROM tb1 WHERE col1='something' OR col2='else'
     UNION 
    SELECT DISTINCT col2 from tb1  WHERE col1='something' OR col2='else'
    Since col1 and col2 can bee either 'something' or 'else',
    the query might just be:
    Code:
    select 'something' as col1
    where exists ( select 1 from tab1 
                   where col1 = 'something' 
                      or col2 = 'something'
    )
    union 
    select 'else' as col1
    where exists ( select 1 from tab1 
                   where col1 = 'else'  
                      or col2 = 'else' 
    )

Posting Permissions

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