1

当我尝试运行此查询时:

select branch_no, max (avg_salary)
from (select allocatedto, avg (salary)
      from staff, worker
      where staff.staff_no = worker.staff_no
      group by allocatedto) 
      as branch_avg (branch_no, avg_salary);

我收到此错误:

Error: near "(": syntax error
4

3 回答 3

1
select my_alias1,my_alias2 from (select col1,col2,...) as A (my_alias1,my_alias2)

上述语法在SQL Server.

要为派生表中的列起别名,您需要在派生表中使用AS。尝试这个

SELECT Max (avg_salary)
FROM   (SELECT allocatedto  AS branch_no,
               Avg (salary) AS avg_salary
        FROM   staff
               INNER JOIN worker
                       ON staff.staff_no = worker.staff_no
        GROUP  BY allocatedto) AS branch_avg;

也开始使用INNER JOIN而不是旧式逗号分隔连接

于 2016-03-04T18:14:13.760 回答
1

在 SQLite 中,子查询上的 AS 子句不能分配列名(而且一开始就不需要它)。

要重命名(子)查询的输出列,您必须在 SELECT 子句中使用 AS:

SELECT branch_no,
       max(avg_salary) AS avg_salary
FROM (...);
于 2016-03-04T18:16:48.920 回答
0

假设 SQL Server 的版本是 2008 R2,

select branch_no, max (avg_salary)
from (select allocatedto, avg (salary)
      from staff, worker
      where staff.staff_no = worker.staff_no
      group by allocatedto) 
    as branch_avg (branch_no, avg_salary)
group by branch_no;
于 2016-03-04T18:18:06.230 回答