Oracle 11g分层查询需要一些继承的数据

时间:2015-01-05 23:26:44

标签: oracle hierarchy

表看起来有点像:

create table taco (
    taco_id     int primary key not null, 
    taco_name   varchar(255), 
    taco_prntid int, 
    meat_id     int,
    meat_inht   char(1)  -- inherit meat
)        

数据如下:

insert into taco values (1, '1',      null,     1,  'N'); 
insert into taco values (2, '1.1',       1,  null,  'Y');
insert into taco values (3, '1.1.1',     2,  null,  'N');
insert into taco values (4, '1.2',       1,     2,  'N');
insert into taco values (5, '1.2.1',     4,  null,  'Y');
insert into taco values (6, '1.1.2',     2,  null,  'Y');

或......

- 1     has a meat_id=1 
- 1.1   has a meat_id=1 because it inherits from its parent via taco_prntid=1
- 1.1.1 has a meat_id of null because it does NOT inherit from its parent 
- 1.2   has a meat_id=2 and it does not inherit from its parent
- 1.2.1 has a meat_id=2 because it does inherit from its parent via taco_prntid=4
- 1.1.2 has a meat_id=1 because it does inherit from its parent via taco_prntid=2

现在......我如何查询每个meat_id taco_id的内容?以下是什么工作,直到我意识到我没有使用继承标志,我的一些数据搞砸了。

select  x.taco_id,  
        x.taco_name, 
        to_number(substr(meat_id,instr(rtrim(meat_id), ' ', -1)+1)) as meat_id 

from    (   select   taco_id, 
                     taco_name,  
                     level-1 "level", 
                     sys_connect_by_path(meat_id, ' ') meat_id
            from     taco
            start    with taco_prntid is null 
            connect  by prior taco_id = taco_prntid 
        ) x        

我可以发布一些失败的尝试来修改我上面的查询,但它们相当令人尴尬的失败。在基础知识之前我还没有使用分层查询,所以我希望有一些我不知道应该搜索的关键字或概念。


我在底部发布了一个答案,以显示我最终得到的结果。我将其他答案视为已被接受,因为他们能够为我提供更清晰的数据,如果没有它,我就不会在任何地方获得。

2 个答案:

答案 0 :(得分:1)

您的内部查询是正确的。当flag为Y时,您只需要从内部查询的meat_id列中选择最右边的数字。 我使用了REGEXP_SUBSTR函数来获取最右边的数字和CASE语句来检查标志。

SQL Fiddle

查询1

select  taco_id,  
        taco_name,
        taco_prntid,
        case meat_inht
            when 'N' then meat_id
            when 'Y' then to_number(regexp_substr(meat_id2,'\d+\s*$'))
        end meat_id,
        meat_inht
from    (   select   taco_id, 
                     taco_name,
                     taco_prntid,
                     meat_id,
                     meat_inht,
                     level-1 "level", 
                     sys_connect_by_path(meat_id, ' ') meat_id2
            from     taco
            start    with taco_prntid is null 
            connect  by prior taco_id = taco_prntid 
        )
order by 1

<强> Results

| TACO_ID | TACO_NAME | TACO_PRNTID | MEAT_ID | MEAT_INHT |
|---------|-----------|-------------|---------|-----------|
|       1 |         1 |      (null) |       1 |         N |
|       2 |       1.1 |           1 |       1 |         Y |
|       3 |     1.1.1 |           2 |  (null) |         N |
|       4 |       1.2 |           1 |       2 |         N |
|       5 |     1.2.1 |           4 |       2 |         Y |
|       6 |     1.1.2 |           2 |       1 |         Y |

查询2

select   taco_id, 
                     taco_name,
                     taco_prntid,
                     meat_id,
                     meat_inht,
                     level-1 "level", 
                     sys_connect_by_path(meat_id, ' ') meat_id2
            from     taco
            start    with taco_prntid is null 
            connect  by prior taco_id = taco_prntid 

<强> Results

| TACO_ID | TACO_NAME | TACO_PRNTID | MEAT_ID | MEAT_INHT | LEVEL | MEAT_ID2 |
|---------|-----------|-------------|---------|-----------|-------|----------|
|       1 |         1 |      (null) |       1 |         N |     0 |     1    |
|       2 |       1.1 |           1 |  (null) |         Y |     1 |     1    |
|       3 |     1.1.1 |           2 |  (null) |         N |     2 |     1    |
|       6 |     1.1.2 |           2 |  (null) |         Y |     2 |     1    |
|       4 |       1.2 |           1 |       2 |         N |     1 |     1 2  |
|       5 |     1.2.1 |           4 |  (null) |         Y |     2 |     1 2  |

答案 1 :(得分:0)

这是我到目前为止所得到的......在接受的答案中应用逻辑之后。我添加了一些其他内容,以便我可以将结果加入到我的meat表中。大写可以稍微优化一下,但我对查询的这一部分是如此......所以它现在必须留下来。

select  x.taco_id,  
        x.taco_name, 
        x.taco_prntname,
        meat_id  

        ,case when to_number(regexp_substr(meat_id,'\d+\s*$'))=0 then null else
            to_number(regexp_substr(meat_id,'\d+\s*$')) end as meat_id 

from    (   select   taco_id, 
                     taco_name,  
                     taco_prntname,
                     level-1 "level", 

                     sys_connect_by_path( 
                        case when meat_inht='N' then nvl(to_char(meat_id),'0') else '' end   
                     ,' ') meat_id 

            from     taco join jobdtl on jobdtl.jobdtl_id=taco.jobdtl_id 
            start    with taco_prntid is null 
            connect  by prior taco_id = taco_prntid 
        ) x  

(你有没有想过,当你读到这样的问题时,真正的架构是什么?显然我不是在开发一个taco项目。或者只要保留一般的关系和概念,它是否有意义?)< / p>