Oracle 分层查询

发布于 2024-08-19 06:25:58 字数 2573 浏览 10 评论 0原文

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

表定义

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

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

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

I have table which holds a company hierarchy. It was decided to use this flat table as there is no defined number of levels in the company. The table workes great, and if you were to use cascading lists on a client it's perfect. However, I need to see a "section", and all other "sections" owned by it. Hopefully the information below will gove you an idea of what I need to be able to do.

Table 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)
)

Data

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

What I need to see

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

I could do this in the C# on the client side, but I would really like to have it as a view on the database.

Can somebody please help me with this. Is it even possible?

If you need anything clarified, please leave a comment and I'll try to provide more information.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妖妓 2024-08-26 06:25:59

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

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

This solution produces a result which resembles the one in the problem specification.

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

The requested solution generates 28 rows when executed against the sample data.

Note that in the sample result, Executive appears as a subsection of itself, while IT, Business Systems, and Operations (which, like Executive, have other subsections too) do not. This solution produces the 3 additional rows.

In addition, note that Executive is its own owner. I am of the belief that cycles should not be permitted in a graph unless the evils they expose us to are the most reasonable way to achieve some required functionality. If there were no such cycle in the graph, the nocycle keyword in the query should be eliminated.

七颜 2024-08-26 06:25:59

是的,这是可能的。您需要使用 Oracle CONNECT BY 语法。 请参阅此处。抱歉没有分享 SQL,因为我自己无法检查它。

Yes, it is possible. You need to use Oracle CONNECT BY syntax. Refer here. Sorry for not sharing the SQL as I can't check it myself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文