0

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:

enter image description here

How can I fix this error?

6
  • your error showing count where your current query is with avg. Commented Jul 17, 2020 at 2:26
  • Your error doesn't match the SQL you posted. Commented Jul 17, 2020 at 2:26
  • Bad habits to kick : using old-style JOINs - that old-style comma-separated list of tables style was replaced with the proper ANSI JOIN syntax in the ANSI-92 SQL Standard (more than 25 years ago) and its use is discouraged Commented Jul 17, 2020 at 4:21
  • AVG(DISTINCT) is basically never the right solution to a problem. What are you trying to calculate? Commented Jul 17, 2020 at 12:47
  • I want to calculate the average salary of pilots and I don't want Repetitious salaries to be in the calculation @GordonLinoff Commented Jul 18, 2020 at 10:47

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You missed the parenthesis for this to run in Access.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.