SQL query to retrieve base members of a hierarchy
Hi, All
Just wanted to share a SQL query I wrote to retrieve base members and parent of a given hierarchy from within a dashboard grid. (Note that the parameter supplies the member ID of the top member in the chosen hierarchy.)
Any thoughts on how to improve are welcome!
With TEST
AS (SELECT childid,
parentID,
dimtypeid,
0 AS level
FROM relationship
WHERE parentid = |!SelectDimension!|
UNION ALL
SELECT ft.childid,
ft.parentID,
ft.dimtypeID,
level + 1
FROM relationship ft
JOIN TEST d
ON ft.parentid = d.childid
)
SELECT m.name As Child_Name,
m.description as Child_Description,
P.name AS Parent_Name,
Description as Parent_Description
FROM TEST d
JOIN relationship a
ON d.parentid = a.parentid
JOIN member AS M
ON a.childID = m.memberid
JOIN member as P
on d.parentid = p.memberid
where NOT EXISTS
(
SELECT NULL FROM TEST d WHERE d.parentID = m.memberID
)
group by m.name,
m.description,
P.name,
p.description