tags:

views:

67

answers:

3
A: 

I don't know which DBMS you're using, but some will get upset that you are using .avg (because avg is reserved) and require you to escape it.

Try changing it from avg to average, just as a test:

SELECT comp_table.average
FROM (SELECT column_1, avg(column_2) as average
      FROM table_1, GROUP BY column_1) comp_table
egrunin
Then the error would've been that the AVG function is missing a parameter. On top of the table alias use...
OMG Ponies
TSQL is the only one I know of that supports square brackets to escape table, column and aliase names, which as Emtucifor pointed out--SQL Server wouldn't have this issue.
OMG Ponies
@OMG Ponies: fixed.
egrunin
+1  A: 

What database server are you using? AVG is a built-in function in all the ones I know of, so you would need to escape it correctly - which depends on database server. In MS SQL Server it's [avg]

Jon
Then the error would've been that the AVG function is missing a parameter. On top of the table alias use...
OMG Ponies
+3  A: 

When the column name is not enclosed in "double quotes", the name is normalized to uppercase; therefore, you were asking for column "AVG", whereas the column name is actually "avg":

select comp_table."avg"
from (select column_1,avg(column_2) as "avg"
  from table_1, group by column_1) comp_table
Jeffrey Kemp
in fact, I double-quote column identifiers only if I'd like to name them in my native language, otherwise I leave them "as is". It would also be a good practice not to use identifiers that match oracle function names like sum or avg, to avoid possible confusions.
be here now
In PostgreSQL enclose a columnname in double quote makes it case-sensitive, ie "mycolunm" differs from "myColumn". I don't know if it's the same for other databases.
M42
In Oracle, it's a bit different. **ALL** columns are specified using double-quotes (in a sense) - it's just that they give us a short-cut that means we can omit the double-quotes if the column only includes uppercase letters and/or underscore characters. It's a common convention, however, to only use the non-quoted syntax for naming columns in Oracle.
Jeffrey Kemp