Groups | Blog | Home
all groups > sql server programming > may 2007 >

sql server programming : How to get the Supervisor information in 1 row


--CELKO--
5/4/2007 6:18:09 PM
Get a copy of TREES & HIERARCHIES IN SQL and look up one of several
better methods for modeling this sort of problem. Google "nested sets
model" for now and your question is trivial.

Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, data types, etc. in
your schema are. Sample data is also a good idea, along with clear
specifications. It is very hard to debug code when you do not let us
see it.

Please stop writing that silly, redundant proprietary "SELECT TOP 100
PERCENT" -- think about what it means and it is going to get dropped
anyway.

Please learn how to name data elements. There is no magical universal
"id" -- it has to be the identifier of a particular entity (not a
physical row). Likewise, you need to tell us "supervisor_<attribute?
[quoted text, click to view]
M A Srinivas
5/4/2007 8:53:02 PM
[quoted text, click to view]

Don't use Sqlect tiop 100 percent . It will be deprecated.
As a quick solution . I think you have managerid in your table along
with id . Not tested

SELECT eu.ID, eu.FirstName, eu.LastName, eu.Supervisor
supervisor.id as SupervisorID, supervisor.firstname as
SupervisorFirstName,
supervisor.lastname as SupervisorLastName, NextLevelManager.id as
NextLevelManagerID,
NextLevelManager.firstname as NextLevelManagerFirstName,
NextLevelManager.lastname as NextLevelManagerLastName

FROM EndoscopyUser.dbo.EndoscopyUser eu
LEFT OUTER JOIN EndoscopyUser.dbo.EndoscopyUser supervisor
ON eu.managerid = supervisor.id
LEFT OUTER JOIN EndoscopyUser.dbo.EndoscopyUser nextlevelmanager
ON supervisor.managerid = nextlevelmanager.id

ORDER BY LastName
zwieback89 via SQLMonster.com
5/4/2007 10:04:02 PM
Hi,

I have a query which goes like this:

SELECT TOP 100 PERCENT ID, FirstName, LastName, Supervisor
FROM EndoscopyUser.dbo.EndoscopyUser eu
ORDER BY LastName

Every Supervisor will also have an entry in the same table with their ID,
FirstName, LastName, Supervisor. I would like to know how I can show upto 2
levels high.

For instance, in 1 row, I see my name, my immediate manager, and my next
level manager (i.e., my immediate's manager's manager)

ID, FirstName, LastName, Supervisor, SupervisorID, SupervisorFirstName,
SupervisorLastName, NextLevelManagerID, NextLevelManagerFirstName,
NextLevelManagerLastName

Can I query again within the same table?

Thanks.

--
---------------------
zwieback89

Message posted via SQLMonster.com
http://www.sqlmonster.com/Uwe/Forums.aspx/sql-server-programming/200705/1
zwieback89 via SQLMonster.com
5/7/2007 3:30:08 PM
Before going to a nested structure algorithim, I was wondering if I can
achieve my results in another way. Here is the DDL with some test data.

Srinivas,
Unfortunately the table is not perfect. Each row has a Supervisor, but not
Supervisor ID.

DDL is:

CREATE TABLE Test (
LawsonID varchar(20) primary key,
FirstName varchar(100),
LastName varchar(100),
Supervisor varchar(100)
)

INSERT INTO Test(LawsonID, FirstName, LastName, Supervisor) Values ('10956',
'Ali','Chowdhury','Pidaparti, Purna')
INSERT INTO Test(LawsonID, FirstName, LastName, Supervisor) Values ('14533',
'Bradley','Rosintoski','Pidaparti, Purna')
INSERT INTO Test(LawsonID, FirstName, LastName, Supervisor) Values ('18778',
'Brian','Emmett','Pidaparti, Purna')
INSERT INTO Test(LawsonID, FirstName, LastName, Supervisor) Values ('21337',
'Sathiya','Rajagopal','Pidaparti, Purna')
INSERT INTO Test(LawsonID, FirstName, LastName, Supervisor) Values ('T2276',
'Aravind','Seshadri','Rajagopal, Sathiya')
INSERT INTO Test(LawsonID, FirstName, LastName, Supervisor) Values ('T2756',
'Polar','Huang','Rajagopal, Sathiya')
INSERT INTO Test(LawsonID, FirstName, LastName, Supervisor) Values ('T951',
'Rishi','Kabra','Rajagopal, Sathiya')
INSERT INTO Test(LawsonID, FirstName, LastName, Supervisor) Values ('T2396',
'Johnson','Abraham','Chowdhury, Ali')
INSERT INTO Test(LawsonID, FirstName, LastName, Supervisor) Values ('T716',
'Kevin','Schmidt','Chowdhury, Ali')
INSERT INTO Test(LawsonID, FirstName, LastName, Supervisor) Values ('24990',
'Purna','Pidaparti','Ileana Rivera')

So the Org Structure is:

Ileana Rivera
Purna Pidaparti
Sathiya Rajagopal
Ali Choudhury

Sathiya and Ali are managers too with people under them.

So using this data, is it possible to get

LawsonID, FirstName, LastName, Supervisor, SupervisorID, SupervisorFirstName,
SupervisorLastName, NextLevelManagerID, NextLevelManagerFirstName,
NextLevelManagerLastName

A supervisor is also an employee, his LawsonID is found within the same table
too.

Thanks.

--
Message posted via http://www.sqlmonster.com
zwieback89 via SQLMonster.com
5/7/2007 7:42:00 PM
I tried to build a stored procedure which would get my results. It is still
in the intial stage:

Alter Procedure BuildOrganization1

AS

Declare @minLawson varchar
Set @minLawson = (Select min(LawsonID) from Test)

INSERT INTO BuildOrganization(LawsonID,FirstName,LastName,
DirectManagerLawsonID,DirectManagerFirstName,DirectManagerLastName,
NextLevelManagerLawsonID,NextLevelManagerFirstName,NextLevelManagerLastName)
SELECT @minLawson, FirstName, Lastname, NULL, NULL, NULL, NULL, NULL, NULL
FROM Test
Where LawsonID = @minLawson

GO

What I find it strange is that when I execute this part:

(Select min(LawsonID) from Test)

It returns me a minimum LawsonID. (i.e., 1 row)

But when I try to run this part:

Declare @minLawson varchar
Set @minLawson = (Select min(LawsonID) from Test)

--INSERT INTO BuildOrganization(LawsonID,FirstName,LastName,
DirectManagerLawsonID,DirectManagerFirstName,DirectManagerLastName,
NextLevelManagerLawsonID,NextLevelManagerFirstName,NextLevelManagerLastName)
SELECT @minLawson, FirstName, Lastname, NULL, NULL, NULL, NULL, NULL, NULL
FROM Test
Where LawsonID = @minLawson

It returns me no rows....

Why would that be so?

THanks.

--
---------------------
zwieback89

Message posted via http://www.sqlmonster.com
AddThis Social Bookmark Button