sub query...

  • hi all,

    How would I build a subquery to return different critiria to 2 seperate columns?

    thanks

    column1 match column1 condition

    column2 match column2 condition

  • 
    
    SELECT a.* FROM TableA a
    WHERE
    EXISTS
    (SELECT * FROM tableB b WHERE a.Column1 = b.column1 AND a.column2 = b.column2)

    or

    
    
    SELECT a.* FROM TableA a
    INNER JOIN tableB b
    ON a.Column1 = b.column1
    AND a.column2 = b.column2

    is this what you meant?

  • The question is a little vague. If I understand, I recommend checking out derived tables. The derived table is simply another select statement that can use parms and return a column result set. The columns can be used in your primary select and/or in the where clause. Once your select is defined, simply place parens around it, qualify it, Now place it in your from clause, of the primary select. In many cases, this is leaps and bounds more efficient than a subquery.

    i.e. SELECT PrimaryTbl.col1, SecondaryTbl.col1

    FROM PrimaryTbl JOIN (derived table select) AS tblB ON PrimaryTbl.colx = TblB.coly.....

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply