如何仅显示第一个条目 'Boss'?
How can I show for the first entry only 'Boss'?
你好,
我正在尝试从给定的 table 中找到每个员工的 superiors/managers,路径应该以 'Boss->' 开头,在每个 [=31= 之后] 应该出现“->”并且路径应该以员工经理的 last_name 结尾。我正在使用递归 CTE,但不知何故,我从查询中获取的数据是错误的。
我有以下结果 table:
id | first_name | last_name | superior_id | path |
----+------------+-----------+-------------+-----------------------------------+
1 | Simon | Dixon | null | Boss->Dixon |
2 | Alfredo | Garza | 1 | Boss->Garza->Garza |
3 | Martin | Lopez | 1 | Boss->Lopez->Lopez |
4 | Jorge | Fox | 1 | Boss->F->Fox |
5 | Isaac | Campbell | 2 | Boss->Garza->Campbell->Campbell |
6 | Rosemary | Mcguire | 3 | Boss->Lopez->Mcguire->Mcguire |
7 | Jake | Griffin | 3 | Boss->Lopez->Griff->Griffin |
10 | Thelma | Lindsey | 4 | Boss->F->Lindsey->Lindsey |
8 | Garrett | Grant | 7 | Boss->Lopez->Griff->Grant->Grant |
9 | Deanna | Olson | 5 | Boss->Garza->Campbell->Ols->Olson |
和我得到上述 table 结果的查询:
WITH RECURSIVE hiearchy AS (
SELECT
id,
first_name,
last_name,
superior_id,
'Boss' AS path
FROM employee
WHERE superior_id IS NULL
UNION
SELECT
employee.id,
employee.first_name,
employee.last_name,
employee.superior_id,
concat(trim(path,'->Dixon'), '->', employee.last_name)
FROM employee join hiearchy
On employee.superior_id=hiearchy.id
)
SELECT
id,
first_name,
last_name,
superior_id,
concat(trim(path,'->Dixon'), '->', last_name) as path
From hiearchy;
有谁能指导我找到解决方案吗?
谢谢。
我认为您想为树中的每个元素生成层次结构路径:
with recursive hiearchy as (
select
id,
first_name,
last_name,
superior_id,
'boss' as path
from employee
where superior_id is null
union all
select
e.id,
e.first_name,
e.last_name,
e.superior_id,
path || '->' || h.last_name
from employee e
join hiearchy h on e.superior_id = h.id
)
select h.* from hiearchy h;
对于您的样本数据,这会生成 a resultset like:
id | first_name | last_name | superior_id | path
-: | :--------- | :-------- | ----------: | :---------------------------
1 | Simon | Dixon | null | boss
2 | Alfredo | Garza | 1 | boss->Dixon
3 | Martin | Lopez | 1 | boss->Dixon
4 | Jorge | Fox | 1 | boss->Dixon
5 | Isaac | Campbell | 2 | boss->Dixon->Garza
6 | Rosemary | Mcguire | 3 | boss->Dixon->Lopez
7 | Jake | Griffin | 3 | boss->Dixon->Lopez
10 | Thelma | Lindsey | 4 | boss->Dixon->Fox
8 | Garrett | Grant | 7 | boss->Dixon->Lopez->Griffin
9 | Deanna | Olson | 5 | boss->Dixon->Garza->Campbell
你好,
我正在尝试从给定的 table 中找到每个员工的 superiors/managers,路径应该以 'Boss->' 开头,在每个 [=31= 之后] 应该出现“->”并且路径应该以员工经理的 last_name 结尾。我正在使用递归 CTE,但不知何故,我从查询中获取的数据是错误的。 我有以下结果 table:
id | first_name | last_name | superior_id | path |
----+------------+-----------+-------------+-----------------------------------+
1 | Simon | Dixon | null | Boss->Dixon |
2 | Alfredo | Garza | 1 | Boss->Garza->Garza |
3 | Martin | Lopez | 1 | Boss->Lopez->Lopez |
4 | Jorge | Fox | 1 | Boss->F->Fox |
5 | Isaac | Campbell | 2 | Boss->Garza->Campbell->Campbell |
6 | Rosemary | Mcguire | 3 | Boss->Lopez->Mcguire->Mcguire |
7 | Jake | Griffin | 3 | Boss->Lopez->Griff->Griffin |
10 | Thelma | Lindsey | 4 | Boss->F->Lindsey->Lindsey |
8 | Garrett | Grant | 7 | Boss->Lopez->Griff->Grant->Grant |
9 | Deanna | Olson | 5 | Boss->Garza->Campbell->Ols->Olson |
和我得到上述 table 结果的查询:
WITH RECURSIVE hiearchy AS (
SELECT
id,
first_name,
last_name,
superior_id,
'Boss' AS path
FROM employee
WHERE superior_id IS NULL
UNION
SELECT
employee.id,
employee.first_name,
employee.last_name,
employee.superior_id,
concat(trim(path,'->Dixon'), '->', employee.last_name)
FROM employee join hiearchy
On employee.superior_id=hiearchy.id
)
SELECT
id,
first_name,
last_name,
superior_id,
concat(trim(path,'->Dixon'), '->', last_name) as path
From hiearchy;
有谁能指导我找到解决方案吗? 谢谢。
我认为您想为树中的每个元素生成层次结构路径:
with recursive hiearchy as (
select
id,
first_name,
last_name,
superior_id,
'boss' as path
from employee
where superior_id is null
union all
select
e.id,
e.first_name,
e.last_name,
e.superior_id,
path || '->' || h.last_name
from employee e
join hiearchy h on e.superior_id = h.id
)
select h.* from hiearchy h;
对于您的样本数据,这会生成 a resultset like:
id | first_name | last_name | superior_id | path -: | :--------- | :-------- | ----------: | :--------------------------- 1 | Simon | Dixon | null | boss 2 | Alfredo | Garza | 1 | boss->Dixon 3 | Martin | Lopez | 1 | boss->Dixon 4 | Jorge | Fox | 1 | boss->Dixon 5 | Isaac | Campbell | 2 | boss->Dixon->Garza 6 | Rosemary | Mcguire | 3 | boss->Dixon->Lopez 7 | Jake | Griffin | 3 | boss->Dixon->Lopez 10 | Thelma | Lindsey | 4 | boss->Dixon->Fox 8 | Garrett | Grant | 7 | boss->Dixon->Lopez->Griffin 9 | Deanna | Olson | 5 | boss->Dixon->Garza->Campbell