plsql - Oracle 分层查询

标签 plsql oracle10g hierarchy hierarchical-data

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

表定义

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# 中执行此操作,但我真的希望将其作为数据库的 View 。

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

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

最佳答案

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

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 关键字。

关于plsql - Oracle 分层查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2133131/

相关文章:

sql - 函数和语句的区别

sql - 带有变量但没有 "SELECT"的简单 "INTO"

sql - 避免冗余更新

java.lang.ClassNotFoundException : sun. jdbc.odbc.JdbcOdbcDriver

performance - 如何降低 select 语句的成本?

javascript - 将过滤应用于层次结构

wordpress:如何为帖子添加层次结构

oracle - 将单行结果集转换为关联数组

sql - 单列上的多重约束

java - 建模类: hierarchy or attribute?