Domanda

I have a CTE (Common Table Expression) that returns employee hierarchy based on level but I need it in a single row with the hierarchy

Level    employeeNumber    employeeName    managernumber
0        1287               Me              789 
1        789                My Boss         345
2        345                His Boss        123
....
10       3                  bla             000

I need to display it as

Level0         Level1         Level2 ... Level10, EmployeeNumber, EmployeeName
Me             My Boss        His Boss            1287            Me

Can it be done with a pivot? seems like pivot need an aggregate function

this is the query,

SELECT  
[0], [1], [2], [3], [4],[5],[6],[7],[8],[9],[10]
FROM
(select [Level], EmployeeNumber,EmployeeName, ManagerNumber from dbo.GetEmployeeHierachy(MYID)) AS SourceTable
PIVOT
(
Max(EmployeeName)
FOR [Level] IN ([0], [1], [2], [3], [4],[5],[6],[7],[8],[9],[10])
) AS PivotTable;

but it returns this

thanks , almost there now i'm getting this

0           1   2                .....
NULL    NULL    His Boss
NULL    NULL    NULL
NULL    NULL    NULL
ME        NULL  NULL
NULL    My Boss NULL
NULL    NULL    NULL   
....

Should be

ME MY Boss His Boss ....

È stato utile?

Soluzione

Ok my fault

I was using to many columns for grouping

SELECT  * FROM
(select [Level],EmployeeName from dbo.GetEmployeeHierachy(MYID)) AS SourceTable
PIVOT
(
Max(EmployeeName) FOR [Level] IN ([0], [1], [2], [3], [4],[5],[6],[7],[8],[9],[10])
) AS PivotTable;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top