尝试将几个查询组合在一起但遇到问题,无论我尝试将它们连接在一起。这是两个的模型:
查询1(获取所有G和相关R):
select distinct
a.g_id,
a.g_name,
b.r_name,
b.r_id
from
g_tab a,
r_tab b,
f_tab c
where
a.g_id = b.g_id
and b.r_id = c.r_id
and c.f_id = 7
order by a.g_id
查询2(遍历层次结构并过滤到特定组):
with parentGs as
(
select
f.g_id,
f.g_name
from
g_tab f
where
f.delete_indicator is null
connect by
prior f.parent_g_id = f.g_id
start with
f.g_id = 22 --DO NOT WANT THIS HARDCODED
)
select
d.g_name
from
parentGs d,
g_group_members_tab e
where
d.g_id = e.member_g_id
and e.g_group_description = 'test'
我不想更改查询1的返回计数,但是想要从查询2加入g_name并将其添加为新的别名列。
以下是我尝试获得的一个示例,但由于g_id被调用而无法正常工作"无效的标识符"在"以"开头。我知道父值只有一个子查询有效,但如果仅使用一个子查询进行重组,仍会得到同样的错误(在"来自"在这种情况下):
select distinct
a.g_id,
a.g_name,
b.r_name,
b.r_id,
(select
d.g_name
from
(select
f.g_id,
f.g_name
from
g_tab f
where
f.delete_indicator is null
connect by
prior f.parent_g_id = f.g_id
start with
f.g_id = a.g_id) d, --THIS GIVES INVALID IDENTIFIER
g_group_members_tab e
where
d.g_id = e.member_g_id
and e.g_group_description = 'test') as parent_g_name
from
g_tab a,
r_tab b,
f_tab c
where
a.g_id = b.g_id
and b.r_id = c.r_id
and c.f_id = 7
order by a.g_id
非常感谢任何帮助加入这两个问题的帮助。我的目标平台是Oracle 11g。非常感谢!
*更新*
以下是一些示例SQL Fiddle数据:
create table g_tab
(
g_id number(9),
g_name varchar2(50),
parent_g_id number(9),
delete_indicator char(1)
)
/
create table g_group_members_tab
(
member_g_id number(9),
g_group_description varchar2(50)
)
/
create table r_tab
(
r_id number(9),
r_name varchar2(50),
g_id number(9)
)
/
create table f_tab
(
f_id number(9),
f_name varchar2(50),
r_id number(9)
)
/
insert into g_tab (g_id, g_name, parent_g_id) values (0, 'a', null)
/
insert into g_tab (g_id, g_name, parent_g_id) values (1, 'b', 0)
/
insert into g_tab (g_id, g_name, parent_g_id) values (2, 'c', 1)
/
insert into g_tab (g_id, g_name, parent_g_id) values (3, 'd', null)
/
insert into g_tab (g_id, g_name, parent_g_id) values (4, 'e', 3)
/
insert into g_tab (g_id, g_name, parent_g_id) values (5, 'f', 4)
/
insert into g_tab (g_id, g_name, parent_g_id) values (6, 'g', null)
/
insert into g_group_members_tab (member_g_id, g_group_description) values (1, 'test')
/
insert into g_group_members_tab (member_g_id, g_group_description) values (3, 'test')
/
insert into r_tab (r_id, r_name, g_id) values (0, 'r', 0)
/
insert into r_tab (r_id, r_name, g_id) values (0, 'r', 1)
/
insert into r_tab (r_id, r_name, g_id) values (0, 'r', 2)
/
insert into r_tab (r_id, r_name, g_id) values (0, 'r', 3)
/
insert into r_tab (r_id, r_name, g_id) values (0, 'r', 4)
/
insert into r_tab (r_id, r_name, g_id) values (1, 'rr', 5)
/
insert into r_tab (r_id, r_name, g_id) values (0, 'r', 6)
/
insert into f_tab (f_id, f_name, r_id) values (7, 'f', 0)
/
insert into f_tab (f_id, f_name, r_id) values (7, 'f', 1)
期望的结果:
g_id g_name r_name r_id parent_g_name
0 a r 0 null
1 b r 0 b
2 c r 0 b
3 d r 0 d
4 e r 0 d
5 f rr 1 d
6 g r 0 null
答案 0 :(得分:0)
只需将子查询转换为内嵌视图,即可完成大部分工作:
select distinct
a.g_id,
a.g_name,
b.r_name,
b.r_id,
d.g_name as parent_g_name
from
g_tab a,
r_tab b,
f_tab c,
(
select
f.g_id,
f.g_name,
connect_by_root f.g_id as root_g_id
from
g_tab f
where
f.delete_indicator is null
connect by
prior f.parent_g_id = f.g_id
) d,
g_group_members_tab e
where
a.g_id = b.g_id
and b.r_id = c.r_id
and c.f_id = 7
and a.g_id = d.root_g_id
and d.g_id = e.member_g_id
and e.g_group_description = 'test'
order by a.g_id
给出了:
G_ID G_NAME R_NAME R_ID PARENT_G_NAME
---------- ------ ------ ---------- -------------
1 b r 0 b
2 c r 0 b
3 d r 0 d
4 e r 0 d
5 f rr 1 d
您错过了空值,因此您需要将其转换为外部联接;但是你需要加入d
到e
。使用ANSI连接时,所有这些都更令人愉快:
select distinct
a.g_id,
a.g_name,
b.r_name,
b.r_id,
d.g_name as parent_g_name
from g_tab a
join r_tab b
on b.g_id = a.g_id
join f_tab c
on c.r_id = b.r_id
left join (
select
e.g_id,
e.g_name,
e.root_g_id
from (
select
f.g_id,
f.g_name,
connect_by_root f.g_id as root_g_id
from
g_tab f
where
f.delete_indicator is null
connect by
prior f.parent_g_id = f.g_id) e
join g_group_members_tab f
on f.member_g_id = e.g_id
where f.g_group_description = 'test'
) d
on d.root_g_id = a.g_id
where c.f_id = 7
order by a.g_id
给出了:
G_ID G_NAME R_NAME R_ID PARENT_G_NAME
---------- ------ ------ ---------- -------------
0 a r 0
1 b r 0 b
2 c r 0 b
3 d r 0 d
4 e r 0 d
5 f rr 1 d
6 g r 0