Unix Technical Forum

How to do this self-join??

This is a discussion on How to do this self-join?? within the SQL Server forums, part of the Microsoft SQL Server category; --> Greetings, Would be thankful for your kind help. I have a table (Northwind employees table). In the employees table, ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 07:05 PM
al
 
Posts: n/a
Default How to do this self-join??

Greetings,

Would be thankful for your kind help. I have a table (Northwind
employees table). In the employees table, a column called ReportsTo
which lists the EmployeeID, int data type, of the manager of an
employee.This ReportsTo column is basically the EmployeeID column:

EmployeeID Title ReportsTo FirstName SecondName ......
......
1 Manager 4 Steven Buchan
2 Sale Rep. 1 Janet Leverling
3 Clerk 1 Ropert King
4 Presedint Null Andrew Fuller

My question is how can I get the column ReportsTo to include first and
second name, instead of numbers (employeeid).

What I want is something like this(firsname, lastname OR lastname,
firstname for the ReportsTo columen):

EmployeeID Title ReportsTo FirstName SecondName
.... ...
1 Manager Andrew Fuller Steven Buchan
2 Sale Rep. Steven Buchan Janet Leverling
3 Clerk Steven Buchan Ropert King
4 Presedint Null Andrew Fuller

MTIA,
Grawsha
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 07:05 PM
Myron
 
Posts: n/a
Default Re: How to do this self-join??

grawsha2000@yahoo.com (al) wrote:

>EmployeeID Title ReportsTo FirstName SecondName ......
>.....
> 1 Manager 4 Steven Buchan
> 2 Sale Rep. 1 Janet Leverling
> 3 Clerk 1 Ropert King
> 4 Presedint Null Andrew Fuller
>
>My question is how can I get the column ReportsTo to include first and
>second name, instead of numbers (employeeid).
>
>What I want is something like this(firsname, lastname OR lastname,
>firstname for the ReportsTo columen):
>
>EmployeeID Title ReportsTo FirstName SecondName
>... ...
> 1 Manager Andrew Fuller Steven Buchan
> 2 Sale Rep. Steven Buchan Janet Leverling
> 3 Clerk Steven Buchan Ropert King
> 4 Presedint Null Andrew Fuller
>
>MTIA,
>Grawsha


Try:
select t.employeeID, t.title, r.firstname+' '+r.lastname as 'Boss',
t.firstname, t.lastname
from testjoin t left outer join testjoin r
on t.reportsto = r.employeeID
order by t.employeeID

HTH,
Myron

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:16 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com