Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 sql query

Author  Topic 

rameshgoudd
Starting Member

13 Posts

Posted - 2007-12-13 : 00:24:52
hi,

i have a sql database table,

fields are

uid,username,reporingmanagerid
1 Ramesh 2
2 Raju 4

3 Kalyani 1


here reporting manager id is nothing but userid.

now i want to display the username and repotingmanger name(nothing ut username ) for that particular user..
how can i write the query for this ...

Kalyani

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-12-13 : 00:53:30
Read about Expanding hierarchies in sql server help file

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

dev2dev
Starting Member

48 Posts

Posted - 2007-12-13 : 11:42:04
try this

select a.uid,a.usename,b.username as managername
from emp a
left outer join emp b
on a.uid = b.reporingmanagerid
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-12-14 : 04:19:39
quote:
Originally posted by dev2dev

try this

select a.uid,a.usename,b.username as managername
from emp a
left outer join emp b
on a.uid = b.reporingmanagerid



think you got it the wrong way round

SELECT 	a.uid, a.usename, b.username AS managername
FROM emp a
left OUTER JOIN emp b
ON b.uid = a.reporingmanagerid



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

dev2dev
Starting Member

48 Posts

Posted - 2007-12-20 : 02:58:20
quote:
Originally posted by khtan

think you got it the wrong way round




first i had problem identifying the difference in yours and my sql

ON 	b.uid = a.reporingmanagerid


when a and b are pointing to same source table,does

b.uid = a.reporingmanagerid

and
a.uid = b.reporingmanagerid


makes any difference? i am just curious
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2007-12-20 : 03:06:35
doesnt make difference in terms of joins but then you need to return

a.usename as managername rather than
b.username as managername as you are always looking for b's reporting manager in a using this

on a.uid = b.reporingmanagerid
Go to Top of Page
   

- Advertisement -