Since you need to return 3 levels of a hierarchy data structure it might be significantly easier to use JOINs instead of using a PIVOT. You would start by returning the parent rows:
select p.departmentName
from dbo.department p
where p.departmentParentID is null;
This will get you all of the top level rows. Then you start adding a join for each additional level that you need, in your case you need a total of three levels so you will add two joins. The final query will be the following:
select p.departmentName department1,
c.departmentName department2,
gc.departmentName department3
from dbo.department p -- parent level
left join dbo.department c -- child level
on p.departmentID = c.departmentParentID
left join dbo.department gc -- grandchild level
on c.departmentID = gc.departmentParentID
where p.departmentParentID is null;
See SQL Fiddle with Demo.
It seems that this will be easier to get the result using a JOIN instead of using the PIVOT. This uses the source data not the recursive view that you created. This gives a result of:
| DEPARTMENT1 | DEPARTMENT2 | DEPARTMENT3 |
|-------------|-------------|--------------|
| depart1 | d1 sub1 | (null) |
| depart2 | d2 sub | (null) |
| depart2 | d2 sub2 | d2s2 subSub1 |
If you want to use a recursive query to get the result, then you could alter your current view slightly to return the 3 columns of names instead of the level numbers:
;with cte as
(
select departmentID,
departmentParentID,
departmentName as Department1,
cast(null as varchar(100)) Department2,
cast(null as varchar(100)) Department3,
1 as Level
from dbo.department
where departmentParentID is null
union all
select d.departmentID,
d.departmentParentID,
Department1,
case when Level + 1 = 2 then d.departmentName else Department2 end,
case when Level + 1 = 3 then d.departmentName else Department3 end,
Level + 1
from dbo.department d
inner join cte h
on d.departmentParentID = h.departmentID
)
select *
from cte;
See SQL Fiddle with Demo. You could then do some filtering using a WHERE clause to return the rows that have all of the department values that you need.