我的表有四列id,名称,名称,manager_id。 表模式:
CREATE TABLE "Employee_Information"
(
"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
"name" varchar,
"designation" varchar,
"manager_id" integer references employee_information(id)
);
如下
ID Name Designation Manager_id
-------------------------------------
1 Raja CEO
2 Mani CTO 1
3 Kavi COO 1
4 Murugan Head 3
5 Alpha Head(Fin) 4
7 Kannan Head 4
员工层次结构如下:
Raja CEO
Mani CTO
Kavi COO
Murugan Head
Alpha Head(Fin)
Kannan Head
Beta CFO
Delta Head
我想要一个SQL查询来显示特定员工的所有可能的经理。他的三年级或其他子级员工姓名不应该在结果集中。
显示同级别或以上的所有其他员工。
我能够找到解决方案。任何帮助表示赞赏。
答案 0 :(得分:1)
你需要一个" Rcursive CTE" (公用表表达式)遍历组织层次结构。像这样:
<强>查询强>
WITH RECURSIVE Emp_CTE (ID, Name, Designation, Manager_id, Manager_name)
AS (
SELECT ID, Name, Designation, Manager_id, cast(NULL as varchar)
FROM Employee_Information
WHERE Manager_ID IS NULL
UNION ALL
SELECT e.ID, e.Name, e.Designation, e.Manager_id, Emp_CTE.Name
FROM Employee_Information e
INNER JOIN Emp_CTE ON Emp_CTE.ID = e.Manager_id
)
SELECT *
FROM Emp_CTE
<强>结果:强>
| ID | Name | Designation | Manager_id | Manager_name |
|----|---------|-------------|------------|--------------|
| 1 | Raja | CEO | null | null |
| 3 | Kavi | COO | 1 | Raja |
| 2 | Mani | CTO | 1 | Raja |
| 4 | Murugan | Head | 3 | Kavi |
| 5 | Alpha | Head(Fin) | 4 | Murugan |
| 7 | Kannan | Head | 4 | Murugan |
<强>设定:强>
CREATE TABLE "Employee_Information" ("id" INTEGER PRIMARY KEY AUTOINCREMENT
NOT NULL, "name" varchar, "designation" varchar, "manager_id" integer references employee_information(id));
INSERT INTO Employee_Information
("ID", "Name", "Designation", "Manager_id")
VALUES
(1, 'Raja', 'CEO', NULL)
;
INSERT INTO Employee_Information
("ID", "Name", "Designation", "Manager_id")
VALUES
(2, 'Mani', 'CTO', '1')
;
INSERT INTO Employee_Information
("ID", "Name", "Designation", "Manager_id")
VALUES
(3, 'Kavi', 'COO', '1')
;
INSERT INTO Employee_Information
("ID", "Name", "Designation", "Manager_id")
VALUES
(4, 'Murugan', 'Head', '3')
;
INSERT INTO Employee_Information
("ID", "Name", "Designation", "Manager_id")
VALUES
(5, 'Alpha', 'Head(Fin)', '4')
;
INSERT INTO Employee_Information
("ID", "Name", "Designation", "Manager_id")
VALUES
(7, 'Kannan', 'Head', '4')
;
查询2
WITH RECURSIVE Emp_CTE (ID, Name, Designation, Manager_id, Manager_name, namepath)
AS (
SELECT ID, Name, Designation, Manager_id, cast(NULL as varchar), name as namepath
FROM Employee_Information
WHERE Manager_ID IS NULL
UNION ALL
SELECT e.ID, e.Name, e.Designation, e.Manager_id, Emp_CTE.Name
, Emp_CTE.namepath || '/' || e.Name
FROM Employee_Information e
INNER JOIN Emp_CTE ON Emp_CTE.ID = e.Manager_id
)
SELECT *
FROM Emp_CTE
结果:
| ID | Name | Designation | Manager_id | Manager_name | namepath |
|----|---------|-------------|------------|--------------|--------------------------|
| 1 | Raja | CEO | null | null | Raja |
| 3 | Kavi | COO | 1 | Raja | Raja/Kavi |
| 2 | Mani | CTO | 1 | Raja | Raja/Mani |
| 4 | Murugan | Head | 3 | Kavi | Raja/Kavi/Murugan |
| 5 | Alpha | Head(Fin) | 4 | Murugan | Raja/Kavi/Murugan/Alpha |
| 7 | Kannan | Head | 4 | Murugan | Raja/Kavi/Murugan/Kannan |
答案 1 :(得分:0)
以下查询将有效:
SELECT Name+' '+Designation AS 'Manager' FROM table1 WHERE ID=(SELECT manager_id FROM table1 WHERE ID='<employee_id>')
如果您通过任何其他语言执行此查询,则只需传递持有employee_id的变量
答案 2 :(得分:-1)
找到解决方案
SELECT M.ID , M.NAME , M.DESIGNATION , N.NAME FROM EMPLOYEE_INFORMATION M
INNER JOIN EMPLOYEE_INFORMATION N ON (M.manager_id<=N.manager_id or M.id=1)
and M.name != N.name where N.ID = <employee_id>;
无论如何,谢谢你的帮助。