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 2005 Forums
 Transact-SQL (2005)
 Need help on SP

Author  Topic 

hariharanpalani
Starting Member

2 Posts

Posted - 2007-07-10 : 13:31:49
I am having an employee table. It's data are hierarchial in manner. This hierarchy is based employee and manager relation.
Each level in the hierarchy is defined as layer. i.e.Root node is defined as Layer 1, followed by layer 2 and so on. Level is another factor here. This was defined using some criteria like salary in which salary between 1000 to 2000 is defined as level 1 and 2000 to 30000 as level 2 and so on. As a result, my table will look like this

----------------------------------------------------------------------------------------
Employee ID Manager Employee ID Layer Level salary
-------------------------------------------------------------------------------------------
10000 NULL 1 1 1000
10001 10000 2 2 2000
10002 10000 2 3 3000
10003 10000 2 4 4000
10004 10001 3 5 5000
10005 10001 3 6 6000
10006 10002 3 7 7000
10007 10002 3 8 8000
10008 10002 3 9 9000
10009 10004 4 10 10000
10010 10004 4 10 10000

From this, i have to create a layer/level metrix output whose output looks like this

--------------------------------------------------------------------------------------------------------------------
Layer 1 2 3 4 5 6 7 8 9 10 Median
/Level
---------------------------------------------------------------------------------------------------------------------
1 1 0 0 0 0 0 0 0 0 0 3

2 0 1 1 1 0 0 0 0 0 0 2.5

3 0 0 0 0 1 1 1 1 1 0 2

4 0 0 0 0 0 0 0 0 0 2 0


In the resultant table, finally we have a median Column. This should be calculate as follows.

1. For each employee in each layer, calculate number of employees under him whose count greater than zero. For e.g. for layer 2(10001,10002,10003) under each manager
number of employees whose count greater than zero are(10001 - 2, 10002 - 3).

2. Count the records for each layer. If count is in even number, take the average of middle to records. For layer 2, (2+3)/2 = 2.5
If count is odd, take the middle value.

Can anyone provide me the SP for this.



hariharanpalani

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-07-10 : 13:43:41
Yes, I could provide a SP for that, but I think you will benefit more from learning yourself.
Have a look at CTE in books online, especially recursive ones. With the flat output, you can group and average the data you want.

Please remember this is a forum where we help out for free. If you need immediate help, consider hiring a professional.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -