Skip to content

Instantly share code, notes, and snippets.

@johndobrien
Created April 26, 2014 00:57
Show Gist options
  • Save johndobrien/11308676 to your computer and use it in GitHub Desktop.
Save johndobrien/11308676 to your computer and use it in GitHub Desktop.
WITH DeptList AS
(select rbc.RbcId as GroupId, CAST(NULL as BIGINT) as ParentId, d.DepartmentId, d.Description, CAST(g.Name as VARCHAR(MAX)) as Name, 1 as level
from relationship r1
INNER JOIN Rbc rbc ON r1.RightId = rbc.RbcId
INNER JOIN Department d on r1.LeftId=d.DepartmentId
INNER JOIN [Group] g on d.DepartmentId=g.GroupId
where r1.RelationshipTypeId = 2
UNION ALL
select DL.GroupId, p.DepartmentId ParentId, c.DepartmentId, c.Description, CAST(DL.Name + '>' + g.Name as VARCHAR(MAX)) as Name, DL.level + 1
from relationship r2
INNER JOIN Department as p ON r2.RightId = p.DepartmentId
INNER JOIN Department as c ON r2.LeftId=c.DepartmentId
INNER JOIN DeptList as DL ON p.DepartmentId = DL.DepartmentId
INNER JOIN [Group] g on c.DepartmentId=g.GroupId)
select * from DeptList;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment