PostgreSQL员工继承缺失值的管理器数据
我试图编写SQL来填补员工的缺失值。经理也是员工。员工缺少的值可以从管理器继承,如果不存在员工经理的值,则将查找经理的经理(如果需要,必须查找root Manager,直到我们获得值)。
以下是表和数据
create table employee(
employee_id int primary key,
name text,
manager_id int,
department text,
lob text);
insert into employee
(employee_id,name,manager_id,department,lob)
values
(12,'a',null,'IT','BFI'),
(3,'b',12,'sales',null),
(4,'c',3,null,'Banking'),
(6,'d',12,null,null),
(7,'e',4,null,null),
(10,'f',7,null,null);
示例数据是
示例:Employee_id = 4 Have Dementment = null and Manager_ID = 3,LOB =银行,此处为该员工的部门为null,因此查询必须从员工的经理继承Department
Department 值。
Employee_id = 4的经理是3,并且具有部门=销售。 现在,员工4的预期结果为
同样必须继承lob
列的值,以替换每个员工的nulls。
示例:员工10和他的经理7都没有部门
和lob
值,但是员工/Manger 7有经理4,经理4具有lob 价值银行和
部门
价值销售从其经理那里继承。 因此,员工10和员工7将继承部门
和lob
值销售和银行业务。 员工10的预期结果为
我的查询
with RECURSIVE inherit_parent_values as (
select * from employee
where employee_id=10
union
select e.manager_id,p.name,p.manager_id,
coalesce(e.department,p.department),
coalesce(e.lob,p.lob)
from employee p inner join
inherit_parent_values e
ON e.manager_id=p.employee_id)
select * from inherit_parent_values
尝试使用递归查询,但这使所有管理人员都将每个员工的根级别升至每个员工的根级别,而不是为每个员工的一行。
还必须在列级中获得无零值。在我的查询中,Cocce似乎没有用,
在PostgreSQL中有可能填充经理人的员工无效价值吗?
预期结果为
I'm trying to write SQL to fill the missing values for an employee. A manager is also an employee. Values missing for an employee can be inherited from manager, if values are not there for an employee's manager, then will lookup to manager's manger ( have to look upto root manager if required, till we get values) values.
Below is table and data
create table employee(
employee_id int primary key,
name text,
manager_id int,
department text,
lob text);
insert into employee
(employee_id,name,manager_id,department,lob)
values
(12,'a',null,'IT','BFI'),
(3,'b',12,'sales',null),
(4,'c',3,null,'Banking'),
(6,'d',12,null,null),
(7,'e',4,null,null),
(10,'f',7,null,null);
sample data is here
example: employee_id=4 has department = null and manager_id=3 and lob=Banking, here department is null for this employee, so query will have to inherit department
value from employee's manager.
employee_id=4's manager is 3 and has department = sales.
now expected result for employee 4 is
Similarly have to inherit values for lob
column to replace nulls for each employee.
Example: neither employee 10 nor his manager 7 doesn't have department
and lob
value, but employee/manger 7 has manager 4 and manager 4 has lob
value Banking and department
value sales inherit from his manager.
So employee 10 and employee 7 will inherit department
and lob
values Sales and Banking.
Expected result for employee 10 is
my query
with RECURSIVE inherit_parent_values as (
select * from employee
where employee_id=10
union
select e.manager_id,p.name,p.manager_id,
coalesce(e.department,p.department),
coalesce(e.lob,p.lob)
from employee p inner join
inherit_parent_values e
ON e.manager_id=p.employee_id)
select * from inherit_parent_values
Trying using recursive query but this is giving all managers rows upto root level of each employee instead of one row for each employee.
Also have to get not null values in column level. seems coalesce is not useful in my query
Is it possible in PostgreSQL to fill employee null values from managers ?
Expected result is
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
164761D1A2B86B5B5BC09BBC09BBFAE6F921D Leaf-员工),我会自上而下(从根部开始 - 顶级经理):
demo: db<>fiddle
You did it bottom-up (starting with the leaf - an employee), I'd do it top-down (starting at the root - a top-manager):