Oracle11g 在ORACLE中使用connectby子句 我有下表: 使用以下数据: 所需输出为:
我已尝试使用“按级别连接” 选择64行Oracle11g 在ORACLE中使用connectby子句 我有下表: 使用以下数据: 所需输出为:,oracle11g,connect-by,Oracle11g,Connect By,我已尝试使用“按级别连接” 选择64行 所以,我不知道我应该使用什么以及如何使用。因为每次角色名称不同,级别也不同。有人能告诉我我遗漏了什么吗?在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
所以,我不知道我应该使用什么以及如何使用。因为每次角色名称不同,级别也不同。有人能告诉我我遗漏了什么吗?在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;
示范条款:
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)
)
在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;
示范条款:
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)
)
@Naveen_Raj_B没有使用带系数的子查询,事实并非如此。使用connect by可能效率不高。caluse模型也有可能。这是家庭作业,你在这里受限制吗?这是一种训练,是的,限制是使用连接方式。感谢您向我介绍递归分解子查询和MODEL子句。如果你也能给我举一个关于MODEL子句的例子,我会很高兴的。@Naveen_Raj_B没有使用带系数的子查询,它不是。使用connect by可能效率不高。caluse模型也有可能。这是家庭作业,你在这里受限制吗?这是一种训练,是的,限制是使用连接方式。感谢您向我介绍递归分解子查询和MODEL子句。如果你也能给我举一个示范条款的例子,我会很高兴的。
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
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;
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)
)
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