Need Help with SELECT Statement

  • Good Afternoon,

    Not sure if this is the correct forum, However I have a questions regarding a SELECT statement. Now I know this syntax is off but it pretty much explains what I am trying to do:

    Two Tables A and B

    Table A Structure:

    User_ID

    FirstName1

    LastName1

    EffectedUser_ID

    Table B Structure:

    User_ID

    FirstName2

    LastName2

    Sample Query:

    ==============================================

    SELECT

    A.User_ID,

    A.FirstName1,

    A.LastName1,

    A.EffectedUser_ID,

    CASE

    WHEN A.EffectedUser_ID = B.User_ID THEN B.FirstName2

    ELSE 'Unknown'

    END

    AS EffectedUserFirstName

    FROM

    A

    LEFT JOIN

    B

    ON

    A.User_ID = B.User_ID

    ===============================================

    Basically I am trying to Have a column that if the User ID Field in Table A matches in Table B then display the First Name of the User in Table B.

    So I get

    COL 1 Col2

    A.EffectedUser_ID B.FirstName2

    IDEAS? CHEERS MATES! :w00t:

  • The syntax is actually perfect.

    You might need to use A.User_ID rather than A.EffectedUser_ID in the CASE statement, I'm not sure which one you need based on your description, but other than that you should be ready to run.

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

  • My Colleague was also able to assist me in this and with your comment I was able to get it right, my ON= Effecteduserid = userid was incorrect haha thank you fro your help sir:hehe:

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

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