Search code examples
sqlsumgroupingaverage

AVG in a SUM statement


In SQL, the following does not work.

SELECT SUM(
   SELECT AVG(2)
)

-- And neither do this:

SELECT SUM(
AVG(2)
)

The output I expected would obviously just be 2. Does anyone know how to work around this?


Solution

  • I can't imagine what your are trying to do but this works:

    SELECT SUM(a)
    FROM (SELECT AVG(2) AS a) AS b