2010-01-25 70 views
4

我有拥有公司层次结构的表。由于公司没有明确的层数,因此决定使用这张平板电脑。这个表很好,如果你在客户端上使用级联列表,它是完美的。但是,我需要看到一个“部分”以及它拥有的所有其他“部分”。希望下面的信息能够帮助你了解我需要做什么。Oracle分层查询

表防守

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 

我可以在次做到这一点e C#在客户端,但我真的很想把它作为数据库的视图。

有人可以帮我这个。它甚至有可能吗?

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

回答

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

+0

非常棒,谢谢。我们的一位DBA提出了一个答案,但它比这个更复杂,非常高效,所以我明天就试试看,并比较解释计划。 – Dave7896 2010-01-25 19:38:00

1

是的,这是可能的。您需要使用Oracle CONNECT BY语法。 Refer here。抱歉没有分享SQL,因为我无法自己查看。

+0

谢谢。我现在正在“连线”,但并未走得太远:-( – Dave7896 2010-01-25 15:17:27