我正在处理此查询并遇到“命令未正确结束”错误。 我发现这是由于我如何为子查询别名。
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 为列名添加别名时,它就可以工作。