我的问题如下。我希望能够使所有在等级上低于给定员工(假设ID为73)的员工,并计算这些员工与他之间的等级级别数。但是,似乎没有任何工作适合我。到目前为止,我已经尝试了这段代码。但是,它似乎仅在id在层次结构中上升时变小时才起作用,这不是这种情况(并且它不计算深度)
select EmployeeKey,
Employeecol
from (select * from Employee
order by Employeecol, EmployeeKey) Employee_sorted,
(select @pv := '73') initialisation
where find_in_set(Employeecol, @pv)
and length(@pv := concat(@pv, ',', EmployeeKey))
表格:
CREATE TABLE [Employee](
[EmployeeKey] [int] NOT NULL,
[ParentEmployeeKey] [int] NULL,
[SalesTerritoryKey] [int] NULL,
[FirstName] [nvarchar](50) NOT NULL,
[LastName] [nvarchar](50) NOT NULL,
[HireDate] [date] NULL,
[Gender] [nchar](1) NULL
)
数据:
1,18,11,"Guy","Gilbert",2006-01-28,"M"
2,7,11,"Kevin","Brown",2006-08-26,"M"
3,14,11,"Roberto","Tamburello",2007-06-11,"M"
4,3,11,"Rob","Walters",2007-07-05,"M"
5,3,11,"Rob","Walters",2007-07-05,"M"
6,267,11,"Thierry","D'Hers",2007-07-11,"M"
7,112,11,"David","Bradley",2007-07-20,"M"
谢谢您的反馈