简体   繁体   中英

Syntax error (missing operator) in query expression in SQL Server

I have some SQL code, and when I run it, I get an error:

SELECT AVG (DISTINCT E.salary) AS avrage
FROM aircraft AS A, certified AS C, employees AS E
WHERE A.cruisingrange > 1000 
  AND A.aid = C.aid 
  AND C.eid = E.eid

Error:

在此处输入图像描述

How can I fix this error?

MS Access does not directly support DISTINCT syntax inside of an aggregate function. One workaround here would be to take the average of a subquery which finds the distinct salaries:

SELECT AVG(salary) AS avrage
FROM
(
    SELECT DISTINCT E.salary
    FROM (aircraft AS A
    INNER JOIN certified AS C ON A.aid = C.aid)
    INNER JOIN employees AS E ON C.eid = E.eid
    WHERE A.cruisingrange > 1000
) t;

Note that I have also converted your old school implicit join syntax to modern explicit join syntax. This is the preferred way of writing SQL these days.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM