Tricky Join query referencing same table twice?

Hi all,

In my registration DB I have a "Users" table and a "Nominations" table. The Nomination table holds two ID's (UserID & NominatingUserID), both of which refer to the "UserID" column in the Users table but different rows. Table structures below:

[dbo].[Nominations](
      [NominationID] [int] IDENTITY(1,1) NOT NULL,
      [UserID] [int] NULL,
      [NominatingUserID] [int] NULL,
      [DateNominated] [datetime] NULL,
      [NominationMessage] [varchar](500) NULL,
      [InviteSent] [varchar](20),
      [IsApproved] [varchar](20)
      
      
[dbo].[Users](
      [UserID] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
      [UserFirstName] [varchar](200) NULL,
      [UserSurname] [varchar](200) NULL,
      [UserEmailAddress] [varchar](200) NULL,
      [UserJobTitle] [varchar](200) NULL,
      [UserOrganisationName] [varchar](200) NULL,
      
      I need to write a query that will return the contents of the Nominations table but replace the Nominations.UserID & Nominations.NominatingUserID fields with the corresponding Users.UserFirstName & Users.UserSurname entries.
      
      That make sense?
      
      Many tahnsk in advance!
      
      Nell
AcclaimNeilrAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chapmandewCommented:
select n.*, u.userfirstname as userfirstname, n.userfirstname as nominatingfirstname
 from nominations n
left join users u on n.userid = u.userid
left join users nom on n.nominatinguserid = nom.userid
0
chapmandewCommented:
just FYI...used left join in case there were nulls.  Include any other fields from the outer joined tables as necessary that you want to return.
0
AcclaimNeilrAuthor Commented:
wow... fast reply! Ta for that.. have tried but get the following error:

"Msg 207, Level 16, State 1, Line 1
Invalid column name 'userfirstname'."

Attempting to step through, not used this syntax before but is it trying to reference a "userfirstname" & "usersurname" fcolumn in th eNominations table as there are none?

Thanks again,

Nell
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

chapmandewCommented:
its trying to reference the UserFirstName columns from the Users table..post the query you got the error with.
0
AcclaimNeilrAuthor Commented:
basically cut and paste yours..

"select n.*, u.userfirstname as userfirstname, n.userfirstname as nominatingfirstname
 from nominations n
left join users u on n.userid = u.userid
left join users nom on n.nominatinguserid = nom.userid"

looks like it referencing th ecorrect table/column names?
0
SuperdaveCommented:
Looks like the n.userfirstname should have been nom.userfirstname:

select n.*, u.userfirstname as userfirstname, nom.userfirstname as nominatingfirstname
 from nominations n
left join users u on n.userid = u.userid
left join users nom on n.nominatinguserid = nom.userid
0
chapmandewCommented:
my fault...I wrote it wrong.  Should be nom.userfirstname rather than n.userfirstname
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AcclaimNeilrAuthor Commented:
Thanks guys, sorry for late response! Have split as Superdave came in with the amendment but obviously chapmandew provided most of it. that ok?

many thanks again!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.