0

我在尝试创建父子层次结构时遇到了麻烦。

询问:

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,但我怎么不做父母子女关系?

Manager 代码中的 nr 1 代表经理,0 是员工。

mssql 服务器 2012

4

1 回答 1

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
于 2015-12-05T18:18:49.117 回答