有没有办法在 Oracle 11g SQL 中为子查询指定别名?
有没有办法给 Oracle 11g 中的子查询一个别名,例如:
select *
from
(select client_ref_id, request from some_table where message_type = 1) abc,
(select client_ref_id, response from some_table where message_type = 2) defg
where
abc.client_ref_id = def.client_ref_id;
否则有没有办法根据 client_ref_id 连接两个子查询。我意识到有一个自连接,但是在我运行的数据库上,自连接可能需要长达 5 分钟才能完成(我正在运行的实际查询中有一些额外的逻辑,但我已经确定自连接是什么导致该问题)。各个子查询只需几秒钟即可自行完成。自连接查询看起来像:
select st.request, st1.request
from
some_table st, some_table st1
where
st.client_ref_id = st1.client_ref_id;
Is there a way to give a subquery in Oracle 11g an alias like:
select *
from
(select client_ref_id, request from some_table where message_type = 1) abc,
(select client_ref_id, response from some_table where message_type = 2) defg
where
abc.client_ref_id = def.client_ref_id;
Otherwise is there a way to join the two subqueries based on the client_ref_id. I realize there is a self join, but on the database I am running on a self join can take up to 5 min to complete (there is some extra logic in the actual query I am running but I have determined the self join is what is causing the issue). The individual subqueries only take a few seconds to complete by them selves. The self join query looks something like:
select st.request, st1.request
from
some_table st, some_table st1
where
st.client_ref_id = st1.client_ref_id;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 CTE(通用表表达式)(又名WITH子句)为查询指定名称或别名,Oracle 也将其称为子查询分解:
You can give a query a name or alias with CTE’s (Common Table Expressions) aka WITH clause aka by Oracle as Subquery Factoring:
我没有要测试的 Oracle 实例,但您发布的内容应该是有效的 ANSI-89 JOIN 语法。这是 ANSI-92 格式的:
I don't have an Oracle instance to test with, but what you posted should be valid ANSI-89 JOIN syntax. Here it is in ANSI-92:
你的查询应该没问题。
另一种选择是:
如果 Oracle 重写查询以使计划保持不变,我不会感到惊讶。
Your query should be fine.
An alternative would be:
I wouldn't be surprised if Oracle rewrote the queries so that the plan would be the same anyway.
在 SQL 标准中,您可以通过以下方式定义子查询的别名
SELECT Temp.patente, Temp.SalarioProm
FROM ( SELECT A.aid 援助, A.patente AS 专利,
AVG (E.esalario) AS SalarioProm
来自 飞机 A、认证 C、员工 E
哪里 A.aid = C.aid
AND C.eid = E.eid
和 A.rangocrucero > 1000
GROUP BY A.aid, A.patente ) AS Temp
其中Temp是子查询的别名
在Oracle中,可以使用WITH子句,特别是当子查询比较复杂,占用大量临时空间。
在 Oracle 中,以下语法可以正常工作
SELECT Temp.patente, Temp.SalarioProm
FROM ( SELECT A.aid 援助, A.patente AS 专利,
AVG (E.esalario) AS SalarioProm
来自 飞机 A、认证 C、员工 E
哪里 A.aid = C.aid
AND C.eid = E.eid
和 A.rangocrucero > 1000
GROUP BY A.aid、A.patente ) 临时
In SQL standard you can define alias for subquery in this way
SELECT Temp.patente, Temp.SalarioProm
FROM ( SELECT A.aid aid, A.patente AS patente,
AVG (E.esalario) AS SalarioProm
FROM Aircraft A, Certified C, Employees E
WHERE A.aid = C.aid
AND C.eid = E.eid
AND A.rangocrucero > 1000
GROUP BY A.aid, A.patente ) AS Temp
Where Temp is the alias for the subquery
In Oracle, you can use the clause WITH, especially when the subquery is complex and use a lot of temporary space.
In Oracle, the following syntax works fine
SELECT Temp.patente, Temp.SalarioProm
FROM ( SELECT A.aid aid, A.patente AS patente,
AVG (E.esalario) AS SalarioProm
FROM Aircraft A, Certified C, Employees E
WHERE A.aid = C.aid
AND C.eid = E.eid
AND A.rangocrucero > 1000
GROUP BY A.aid, A.patente ) Temp