0

我有下表:

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;

我曾尝试使用按级别连接。

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 行

所以,我看不到我应该使用什么以及如何使用。因为级别每次都随着角色名称而变化。那么谁能告诉我我错过了什么?

4

3 回答 3

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) 
)
于 2013-03-05T14:17:59.633 回答
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;
于 2013-08-15T12:46:10.897 回答
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
于 2015-05-21T07:48:38.653 回答