PostgreSQL员工继承缺失值的管理器数据

发布于 2025-02-06 21:18:38 字数 2141 浏览 1 评论 0原文

我试图编写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);

enter image description here

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

enter image description here

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

enter image description here

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

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故笙诉离歌 2025-02-13 21:18:38

164761D1A2B86B5B5BC09BBC09BBFAE6F921D Leaf-员工),我会自上而下(从根部开始 - 顶级经理):

WITH RECURSIVE rec AS (
    SELECT *
    FROM employee
    WHERE manager_id IS NULL
    
    UNION
    
    SELECT 
        e.employee_id,
        e.name,
        e.manager_id,
        COALESCE(e.department, r.department) as department,
        COALESCE(e.lob, r.lob) as lob
    FROM employee e
    JOIN rec r ON r.employee_id = e.manager_id
)
SELECT * FROM rec

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):

WITH RECURSIVE rec AS (
    SELECT *
    FROM employee
    WHERE manager_id IS NULL
    
    UNION
    
    SELECT 
        e.employee_id,
        e.name,
        e.manager_id,
        COALESCE(e.department, r.department) as department,
        COALESCE(e.lob, r.lob) as lob
    FROM employee e
    JOIN rec r ON r.employee_id = e.manager_id
)
SELECT * FROM rec
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文