Help with SQL Code

  • I have a Employee Table with SupervisorID which shows the Supervisor Employee number

    and I want to show Supervisor Name from the EmployeeID and Employee field.

    SELECT EmployeeID, FirstName+' '+LastName AS Employee,

    SupervisorID, SiteID, HireDate

    FROM dbo.Employee

  • Here is one way to do it:

    SELECT EmployeeID

    , FirstName+' '+LastName AS Employee

    , (Select s.FirstName+' '+s.LastName

    From dbo.Employee s

    Where s.EmployeeID = e.SupervisorID) as Supervisor

    , SiteID

    , HireDate

    FROM dbo.Employee e

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • Here's another

    SELECT e.EmployeeID,

    e.FirstName + ' ' + e.LastName AS Employee,

    s.FirstName + ' ' + s.LastName as Supervisor,

    e.SiteID,

    e.HireDate

    FROM dbo.Employee e

    LEFT OUTER JOIN dbo.Employee s ON e.SupervisorID = s.EmployeeID

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • 😀 Thank You it worked

    I appricate the help...

  • Glad we could help.

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

Viewing 5 posts - 1 through 4 (of 4 total)

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