combine queries

  • Hi all,

    I had to write two separate queries to get what I want: that is, knowing the branchCode, get the email addresses from another table for two columns. This query gives me the email addresses:

    SELECT p.EMail

    FROM PEOPLE p

    WHERE p.ID IN ('SU00U10', 'SU00TY6')

    and this one gives me the ID's:

    SELECT ApproverID, AlternatesID

    FROM Branches

    WHERE branchCode = 'E2'

    so I thought I could insert the 2nd statement inside the parenthesis of the first statement, like so:

    SELECT p.InternetMailbox

    FROM iadb_dev.dbo.tbl_PEOPLE p

    WHERE p.UniqueID =

    (SELECT ApproverID, AlternatesID

    FROM emh.dbo.tblEVBranchesCopy

    WHERE branchCode = 'EV1')

    but the code above gives error: Msg 116, Level 16, State 1, Line 1

    Only one expression can be specified in the select list when the subquery is not introduced with EXISTS. I couldn't get it to work except by writing two queries:

    SELECT p.EMail

    FROM PEOPLE p

    WHERE p.ID =

    (SELECT ApproverID FROM Branches WHERE branchCode = 'E2')

    SELECT p.EMail

    FROM PEOPLE p

    WHERE p.ID =

    (SELECT AlternatesID FROM Branches WHERE branchCode = 'E2')

    It works, but would one query be more efficient and still easy to read? good enough? suggestions?

  • kubio (11/4/2008)


    Hi all,

    I had to write two separate queries to get what I want: that is, knowing the branchCode, get the email addresses from another table for two columns. This query gives me the email addresses:

    SELECT p.EMail

    FROM PEOPLE p

    WHERE p.ID IN ('SU00U10', 'SU00TY6')

    and this one gives me the ID's:

    SELECT ApproverID, AlternatesID

    FROM Branches

    WHERE branchCode = 'E2'

    so I thought I could insert the 2nd statement inside the parenthesis of the first statement, like so:

    but the code above gives error: Msg 116, Level 16, State 1, Line 1

    Only one expression can be specified in the select list when the subquery is not introduced with EXISTS. I couldn't get it to work except by writing two queries:

    SELECT p.EMail

    FROM PEOPLE p

    WHERE p.ID =

    (SELECT ApproverID FROM Branches WHERE branchCode = 'E2')

    SELECT p.EMail

    FROM PEOPLE p

    WHERE p.ID =

    (SELECT AlternatesID FROM Branches WHERE branchCode = 'E2')

    It works, but would one query be more efficient and still easy to read? good enough? suggestions?

    How about:

    SELECT a.ApproverID, a.AlternatesID

    , p.UniqueID , p.InternetMailbox

    FROM iadb_dev.dbo.tbl_PEOPLE p

    left join emh.dbo.tblEVBranchesCopy a

    on (p.UniqueID = a.approverID

    or p.UniqueID = a.AlternatesID )

    and a.branchCode = 'EV1'

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • sure, change and to where and it's perfect

    thank you.

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

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