我在尝试创建父级和子级层次结构时遇到了麻烦。
查询:
SELECT
ID,
[Manager Code],
Manager,
CASE WHEN [Manager Code] = 1 then
LAG(ID) over (order by [Manager Code] desc, ID, p_id) end as IDParentChild,
ROW_NUMBER() over (partition by ID order by [Manager Code] desc, ID, p_id) as RowNum
FROM Employee
WHERE ID IN ('90110000000','90110100000','90400000000')
表:
我有父和IDParentChild的ID,但我怎么不做父子关系?。
经理代码中的nr 1代表经理,0代表雇员。
mssql server 2012
答案 0 :(得分:0)
试试这个:
SELECT *,
CASE WHEN t.ManagerCode = 1 THEN
(SELECT max(manager) FROM yourTbl t1 WHERE t1.ID < t.ID AND ManagerCode = 1)
ELSE
(SELECT max(manager) FROM yourTbl t1 WHERE t1.ID = t.ID AND ManagerCode = 1)
END AS ParentManager
FROM yourTbl t