为什么在 Oracle SQL 中为子查询添加别名时不能使用 AS?

问题描述 投票:0回答:1

我正在处理此查询并遇到“命令未正确结束”错误。 我发现这是由于我如何为子查询别名。

select ctable.code, company.founder, ctable.lman, ctable.sman, ctable.man, ctable.emp
from (select 
c.company_code as code, 
count(distinct l.lead_manager_code) as lman, 
count(distinct s.senior_manager_code) as sman,
count(distinct m.manager_code) as man,
count(distinct e.employee_code) as emp
from company c join lead_manager l on c.company_code = l.company_code
join senior_manager s on c.company_code = s.company_code
join manager m on c.company_code = m.company_code
join employee e on c.company_code = e.company_code 
group by c.company_code) AS ctable 
join company on company.company_code = ctable.code
order by ctable.code;

我发现如果我删除子查询末尾的“AS”,错误就会消失,如下所示:

select ctable.code, company.founder, ctable.lman, ctable.sman, ctable.man, ctable.emp
from (select 
c.company_code as code, 
count(distinct l.lead_manager_code) as lman, 
count(distinct s.senior_manager_code) as sman,
count(distinct m.manager_code) as man,
count(distinct e.employee_code) as emp
from company c join lead_manager l on c.company_code = l.company_code
join senior_manager s on c.company_code = s.company_code
join manager m on c.company_code = m.company_code
join employee e on c.company_code = e.company_code 
group by c.company_code) ctable 
join company on company.company_code = ctable.code
order by ctable.code;

据我所知,AS 运算符只是一种以更详细的方式设置别名的方法,那么为什么在尝试使用 AS 与不使用 AS 为子查询设置别名时会失败呢? 当我在子查询中使用 AS 为列名添加别名时,它就可以工作。

sql oracle join subquery alias
1个回答
0
投票

语法不允许。

来自

SELECT
文档

table_reference::= 

Syntax diagram for a table reference

AS
query_table_expression
之间没有
t_alias

© www.soinside.com 2019 - 2024. All rights reserved.