我想要所有的孙子, 以下逻辑适用于我,但由于这是一个视图,我无法传递参数xxx
SQL代码:
WITH grandchildren as
(
SELECT e1.ID ID, e1.LName + ', '+e1.FName FullName, 0 as lvl
FROM lstEmp e1
WHERE e1.ID ='xxx'
UNION ALL
SELECT e2.ID, e2.LName + ', '+e2.FName, lvl+1
FROM lstEmp e2
INNER JOIN grandchildren g ON e2.Supervisor = g.LsuID
)
SELECT *
FROM grandchildren
ORDER BY lvl, ID;
我需要以where
条件获得此输出吗?
答案 0 :(得分:0)
在where
内或grandchildren
下方插入from
,这应该可行。
WITH grandchildren as
(
SELECT e1.ID ID, e1.LName + ', '+e1.FName FullName, 0 as lvl
FROM lstEmp e1
WHERE e1.ID ='xxx'
UNION ALL
SELECT e2.ID, e2.LName + ', '+e2.FName, lvl+1
FROM lstEmp e2
WHERE e2.LName = 'nameToFilter'
INNER JOIN grandchildren g ON e2.Supervisor = g.LsuID
)
SELECT *
FROM grandchildren
ORDER BY lvl, ID;