我正在使用Postgres 9.6而我正在尝试从父母关系表中重新创建关卡结构。
我使用了WITH RECURSIVE查询;我想要做的是获取每个employee_id
最高级别的那个,但我搞砸了一些东西,因为尽管使用DISTINCT
,仍然有重复的行。
代码:
-- source table 1
nov=# select comp_user_id, id from corp_companyuserprofile limit 10;
comp_user_id | id
------------------+----
ffadmin | 1
clientadmin | 4
ca | 5
GGAdmin bb . | 9
comp_pa | 10
DEONI2 | 11
RNoeleded | 12
asuhguhbnd | 13
KxxxxChan | 14
nmwaffmin | 15
-- source table 2
nov=# select * from company_managerrelation limit 10;
id | mgr_type | employee_id | manager_id
------+----------+-------------+------------
3932 | mgr | 485 | 15
3933 | mgr | 526 | 527
3934 | ind_mgr | 351 | 304
3935 | mgr | 617 | 619
3936 | ind_mgr | 184 | 43
3937 | mgr | 1033 | 1023
3938 | ind_mgr | 312 | 25
3939 | mgr | 566 | 541
3940 | mgr | 394 | 390
3941 | mgr | 438 | 462
-- getting hierarchical structure with levels
with recursive cup_from_parents as
(
select c.id, c.comp_user_id, '{}' :: int [] as parents, 0 as level,
m.employee_id, m.manager_id
from corp_companyuserprofile c LEFT JOIN corp_managerrelation m
on c.id = m.employee_id
where m.manager_id is NULL
union all
select c.id, c.comp_user_id, parents || m.manager_id, level+1,
m.employee_id, m.manager_id
from cup_from_parents p
join corp_managerrelation m
on m.manager_id = p.id
join corp_companyuserprofile c
on c.id = m.employee_id
where not c.id = any(parents)
)
select gg.id, gg.comp_user_id, gg.parents, max(gg.level) as max_level from (
select distinct on (cfp.id, cfp.parents, cfp.comp_user_id, cfp.level) cfp.id, cfp.comp_user_id, cfp.parents, cfp.level
from cup_from_parents cfp
join corp_managerrelation cmr
on cfp.id = cmr.employee_id
) as gg
group by (gg.id, gg.comp_user_id, gg.parents, gg.level)
order by gg.level DESC;
id | comp_user_id | parents | max_level
-----+--------------+-----------------------------------------------------+-----------
521 | FAUCHMY1 | {13,283,24,296,532,531,530,529,528,527,526,525,523} | 13
522 | LARAMMA1 | {13,283,24,296,532,531,530,529,528,527,526,525,523} | 13
521 | FAUCHMY1 | {13,24,296,532,531,530,529,528,527,526,525,523} | 12
521 | FAUCHMY1 | {13,283,24,296,531,530,529,528,527,526,525,523} | 12
521 | FAUCHMY1 | {13,283,24,296,532,530,529,528,527,526,525,523} | 12
521 | FAUCHMY1 | {13,283,24,296,532,531,529,528,527,526,525,523} | 12
521 | FAUCHMY1 | {13,283,24,296,532,531,530,528,527,526,525,523} | 12
521 | FAUCHMY1 | {13,283,24,296,532,531,530,529,527,526,525,523} | 12
521 | FAUCHMY1 | {13,283,24,296,532,531,530,529,528,526,525,523} | 12
521 | FAUCHMY1 | {13,283,24,296,532,531,530,529,528,527,525,523} | 12
521 | FAUCHMY1 | {13,283,24,296,532,531,530,529,528,527,526,523} | 12
521 | FAUCHMY1 | {13,283,24,296,532,531,530,529,528,527,526,525} | 12
521 | FAUCHMY1 | {13,283,24,532,531,530,529,528,527,526,525,523} | 12
521 | FAUCHMY1 | {13,283,296,532,531,530,529,528,527,526,525,523} | 12
522 | LARAMMA1 | {13,24,296,532,531,530,529,528,527,526,525,523} | 12
522 | LARAMMA1 | {13,283,24,296,531,530,529,528,527,526,525,523} | 12
522 | LARAMMA1 | {13,283,24,296,532,530,529,528,527,526,525,523} | 12
522 | LARAMMA1 | {13,283,24,296,532,531,529,528,527,526,525,523} | 12
522 | LARAMMA1 | {13,283,24,296,532,531,530,528,527,526,525,523} | 12
522 | LARAMMA1 | {13,283,24,296,532,531,530,529,527,526,525,523} | 12
522 | LARAMMA1 | {13,283,24,296,532,531,530,529,528,526,525,523} | 12
522 | LARAMMA1 | {13,283,24,296,532,531,530,529,528,527,525,523} | 12
...
对此有何帮助?