Question

I have the following table:

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

WITH THE FOLLOWING DATA:

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);

THE DESIRED OUTPUT IS:

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;

I have tried to use connect by and level.

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

AND THE OUTPUT IS:

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 rows selected

So, i cannot see what and how exactly i should use. Because the level varies with role_name everytime. So can anyone tell me what i am missing?

Was it helpful?

Solution

in 11g just use a recursive factored sub query:

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;

if you want a connect by approach (not as good as the above), then:

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;

model clause:

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) 
)

OTHER TIPS

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;
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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top