基于两个表的层次结构查询,

时间:2015-12-18 22:50:01

标签: oracle

在我的oracle 11g数据库中,我有两个表。第一个accitem包含项目信息,第二个acchart包含图表信息。表accitem中的每一行都是唯一的。没有重复。在另一个表中,acchart item_type引用acchart表中的外键。它看起来像这样:

Accitem
ID         Details
BS         Balance sheet
PL         Profit & loss

acchart

item_code    item_description       item_type
INC          Income                  PL
EXP          Expenses                PL
Ass          Assets                  BS
Eqt          Equity                  BS

我想要实现的目标如下:

Balance Sheet
    + Assets
    + Equity

Profit & Loss
   + Income
   + Expenses

你将如何实现这一目标?

1 个答案:

答案 0 :(得分:0)

您确实可以使用connect by

select decode(level, 2, ' + ', '')||q.name from
(
select ID, Details name, null parent_id
from accitem
UNION ALL
select null ID, item_description name, item_type parent_id
from acchart
) q
start with parent_id is null
connect by prior id = parent_id

但是在较大的数据集上,它的性能值得怀疑。

相关问题