Wednesday, March 7, 2012

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"al" <grawsha2000@.yahoo.com> wrote in message
news:66edfd3c.0312042257.1b0ed9d2@.posting.google.c om...
> 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

SELECT E1.EmployeeID,
E1.Title,
E2.FirstName + ' ' + E2.LastName AS ReportsTo,
E1.FirstName,
E1.LastName
FROM Northwind..Employees AS E1
LEFT OUTER JOIN
Northwind..Employees AS E2
ON E2.EmployeeID = E1.ReportsTo

Regards,
jag|||"John Gilson" <jag@.acm.org> wrote in message news:<C9%zb.346261$pT1.293362@.twister.nyc.rr.com>...
> "al" <grawsha2000@.yahoo.com> wrote in message
> news:66edfd3c.0312042257.1b0ed9d2@.posting.google.c om...
> > 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
> SELECT E1.EmployeeID,
> E1.Title,
> E2.FirstName + ' ' + E2.LastName AS ReportsTo,
> E1.FirstName,
> E1.LastName
> FROM Northwind..Employees AS E1
> LEFT OUTER JOIN
> Northwind..Employees AS E2
> ON E2.EmployeeID = E1.ReportsTo
> Regards,
> jag

Jag,

Thanks for your help. Does this solution work with CommandBuilder in ADO.NET?

Grawsha

No comments:

Post a Comment