代码之家  ›  专栏  ›  技术社区  ›  Dave7896

Oracle分层查询

  •  4
  • Dave7896  · 技术社区  · 15 年前

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

    表DEF

    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中执行此操作,但我真的希望将它作为数据库上的视图。

    有人能帮我一下吗?有可能吗?

    如果您需要任何澄清,请留下评论,我将尽力提供更多信息。

    2 回复  |  直到 15 年前
        1
  •  3
  •   Vadim K.    15 年前

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

    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 作为自身的一个部分出现,而 IT , Business Systems Operations (像 执行官 ,也有其他小节)不要。此解决方案将生成另外3行。

    此外,请注意 行政人员 是它自己的主人。我相信周期不应该被允许出现在一个图表中,除非它们暴露给我们的邪恶是实现某些所需功能的最合理的方法。如果图表中没有这样的循环, nocycle 应删除查询中的关键字。

        2
  •  1
  •   Guru    15 年前

    是的,这是可能的。你需要使用甲骨文 CONNECT BY 语法。 Refer here . 很抱歉没有共享SQL,因为我自己无法检查它。