在ORACLE中使用CONNECT BY子句

时间:2013-03-05 14:01:54

标签: oracle11g connect-by

我有下表:

CREATE TABLE TASK_2
(
 ROLE_NAME VARCHAR2(50 BYTE),
 MIN_CNT   NUMBER,
 MAX_CNT   NUMBER
)

以下数据:

INSERT INTO TASK_2 VALUES ( 'SE', 3, 5);
INSERT INTO TASK_2 VALUES ( 'SSE', 2, 6);
INSERT INTO TASK_2 VALUES ( 'MGR', 3, 5);
INSERT INTO TASK_2 VALUES ( 'SR_MGR', 1, 4);

所需的输出是:

se there are 3;

se there are 4;

se there are 5;

sse there are 2;

sse there are 3;

sse there are 4;

sse there are 5;

sse there are 6;

mgr there are 3;

mgr there are 4;

mgr there are 5;

sr_mgr there are 1;

sr_mgr there are 2;

sr_mgr there are 3;

sr_mgr there are 4;

我尝试过使用connect by和level。

SELECT role_name||' there are '||level
FROM task_2
CONNECT BY level < max_cnt
AND level          > min_cnt
ORDER BY role_name;

和输出是:

mgr there are 4                                                                                       
mgr there are 4                                                                                       
mgr there are 4                                                                                       
mgr there are 1                                                                                       
mgr there are 4                                                                                       
mgr there are 4                                                                                       
mgr there are 4                                                                                       
mgr there are 4                                                                                       
mgr there are 4                                                                                       
se there are 4                                                                                        
se there are 4                                                                                        
se there are 4                                                                                        
se there are 1                                                                                        
se there are 4                                                                                        
se there are 4                                                                                        
se there are 4                                                                                        
se there are 4                                                                                        
se there are 4                                                                                        
sr_mgr there are 2                                                                                    
sr_mgr there are 3                                                                                    
sr_mgr there are 2                                                                                    
sr_mgr there are 3                                                                                    
sr_mgr there are 2                                                                                    
sr_mgr there are 1                                                                                    
sr_mgr there are 3                                                                                    
sr_mgr there are 2                                                                                    
sr_mgr there are 3                                                                                    
sse there are 5                                                                                       
sse there are 5                                                                                       
sse there are 5                                                                                       
sse there are 5                                                                                       
sse there are 5                                                                                       
sse there are 5                                                                                       
sse there are 3                                                                                       
sse there are 3                                                                                       
sse there are 4                                                                                       
sse there are 5                                                                                       
sse there are 4                                                                                       
sse there are 5                                                                                       
sse there are 5                                                                                       
sse there are 5                                                                                       
sse there are 5                                                                                       
sse there are 5                                                                                       
sse there are 5                                                                                       
sse there are 4                                                                                       
sse there are 5                                                                                       
sse there are 5                                                                                       
sse there are 5                                                                                       
sse there are 4                                                                                       
sse there are 5                                                                                       
sse there are 3                                                                                       
sse there are 4                                                                                       
sse there are 5                                                                                       
sse there are 4                                                                                       
sse there are 5                                                                                       
sse there are 5                                                                                       
sse there are 5                                                                                       
sse there are 1                                                                                       
sse there are 5                                                                                       
sse there are 5                                                                                       
sse there are 3                                                                                       
sse there are 5                                                                                       
sse there are 4                                                                                       
sse there are 4                                                                                       

选择64行

所以,我看不出我应该使用什么以及如何使用。因为级别因role_name而异。那么有人能告诉我我错过了什么吗?

3 个答案:

答案 0 :(得分:3)

11g中的

只使用递归因子查询:

with data (role_name ,  max_cnt, val)
as (select role_name , max_cnt, min_cnt
      from task_2
    union all
    select role_name , max_cnt, val+1
      from data
     where val+1 <= max_cnt)
select role_name || ' there are ' || val
  from data
 order by role_name, val;

如果您想要connect by方法(不如上述方法),那么:

select role_name || ' there are ' || (min_cnt + d.r - 1)
  from task_2 t
       cross join (select rownum r 
                     from dual 
                   connect by level <= (select max(max_cnt - min_cnt + 1) 
                                          from task_2)) d
 where d.r <= max_cnt - min_cnt + 1
 order by role_name, d.r;

模型条款:

with data as (select role_name, min_cnt, max_cnt, max_cnt-min_cnt+1 vals
                from task_2)
select role_name|| ' there are ' || vals
  from data
model
partition by (role_name)
dimension by (0 as i)
measures (vals, min_cnt)
rules (
  vals[for i from 0 to vals[0] increment 1] = min_cnt[0] + cv(i) 
)

答案 1 :(得分:0)

select a.role_name||' there are '|| b.n Txt from TASK_2 a,
(select rownum n from dual connect by level <= (select sum(max_cnt) from TASK_2)) b
where b.n >= min_cnt and b.n <= max_cnt
order by a.role_name,b.n;

答案 2 :(得分:0)

with TASK_2  as ( 
    select 'SE' as ROLE_NAME , 3 as MIN_CNT   , 5 as MAX_CNT    from dual union
    select 'SSE', 2, 6 from dual union
    select 'MGR', 3, 5 from dual union
    select  'SR_MGR', 1, 4 from dual 
)

select t.ROLE_NAME ||' there are '||l.lv||';' as txt from TASK_2  t left join
(select level as lv from dual connect by level <= (select max(MAX_CNT)  from TASK_2)) l
on l.lv between t.MIN_CNT and t.MAX_CNT
order by txt