4

我有一个包含公司层次结构的表。决定使用此平面表,因为公司中没有定义级别的数量。该表效果很好,如果您要在客户端上使用级联列表,那就完美了。但是,我需要查看一个“部分”,以及它拥有的所有其他“部分”。希望下面的信息能让您了解我需要做什么。

表定义

create table SECTION
(
  SECTION_ID       NUMBER(38) not null,
  SECTION_NAME     VARCHAR2(75) not null,
  SECTION_MANAGER  NUMBER(6) not null,
  SECTION_LEVEL    NUMBER(3) not null,
  OWNER_SECTION_ID NUMBER(38)
)

数据

1   IT                    901763    2   0
2   Business Systems             904241 3   1
3   Business Analysis            900813 4   2
4   Development          900976 4   2
5   Testing                   907052    4   2
6   Systems Architecture    908012  4   2
7   Mobilisation             904241 4   2
8   Operations           900885 2   0
9   Area 2                    900456    3   8
0   Executive                          1    0   0

我需要看到的

0   Executive                          1    8   Operations
0   Executive                          1    1   IT
0   Executive                          1    0   Executive
0   Executive                          1    2   Business Systems
0   Executive                          1    7   Mobilisation
0   Executive                          1    6   Systems Architecture
0   Executive                          1    4   Development
0   Executive                          1    3   Business Analysis
0   Executive                          1    5   Testing
0   Executive                          1    9    Area 2
1   IT                    901763    2   Business Systems
1   IT                    901763    7   Mobilisation
1   IT                    901763    6   Systems Architecture
1   IT                    901763    4   Development
1   IT                    901763    3   Business Analysis
1   IT                    901763    5   Testing
2   Business Systems             904241 7   Mobilisation
2   Business Systems             904241 6   Systems Architecture
2   Business Systems             904241 4   Development
2   Business Systems             904241 3   Business Analysis
2   Business Systems             904241 5   Testing
8   Operations           900885 9    Area 2
7   Mobilisation             904241     
6   Systems Architecture    908012      
4   Development          900976     
3   Business Analysis            900813     
5   Testing                   907052        
9    Area 2                   900456

我可以在客户端的 C# 中执行此操作,但我真的很想将它作为数据库的视图。

有人可以帮我解决这个问题。甚至可能吗?

如果您需要澄清任何事情,请发表评论,我会尽力提供更多信息。

4

2 回答 2

3

此解决方案产生的结果类似于问题规范中的结果。

select
    connect_by_root section_id section_id,
    connect_by_root section_name section_name,
    connect_by_root section_manager section_manager,
    section_id subsection_id,
    section_name subsection_name
from
    section
connect by nocycle
    prior section_id = owner_section_id

请求的解决方案在针对样本数据执行时生成 28 行。

请注意,在示例结果中,Executive显示为自身的一个子部分,而ITBusiness SystemsOperations(与 一样Executive,也有其他子部分)没有。此解决方案产生 3 个额外的行。

另外,请注意Executive是它自己的所有者。我相信循环不应该在图中被允许,除非它们暴露给我们的邪恶是实现某些所需功能的最合理的方式。如果图中没有这样的循环,nocycle则应删除查询中的关键字。

于 2010-01-25T19:19:19.197 回答
1

对的,这是可能的。您需要使用 OracleCONNECT BY语法。参考这里。很抱歉没有共享 SQL,因为我自己无法检查。

于 2010-01-25T15:14:49.347 回答