SQL error: invalid identifier

I am new to SQL, and I would really like to help solve this rather simple problem.

select comp_table.*
from (select column_1,avg(column_2) as "avg"
      from table_1, group by column_1) comp_table

→ returns the correct entries with two columns named column_1andavg

But if I go to:

select comp_table.avg
from (select column_1,avg(column_2) as "avg"
      from table_1, group by column_1) comp_table

→ returns Error: invalid identifier "avg"

The thing is, I only need to select a column avg, so I cannot do it select comp_table.*. Can you guys help?

Also, if you can kindly provide some tips on customizing your query, that would be awesome.

+3
source share
3 answers

" ", ; "AVG", "avg":

select comp_table."avg"
from (select column_1,avg(column_2) as "avg"
  from table_1, group by column_1) comp_table
+4

? AVG - , , - . MS SQL Server [avg]

+1

I don’t know which DBMS you are using, but some will be frustrated by what you are using .avg(because it is avgreserved) and require you to avoid it.

Try changing it from avgto average, like a test:

SELECT comp_table.average
FROM (SELECT column_1, avg(column_2) as average
      FROM table_1, GROUP BY column_1) comp_table
0
source

Source: https://habr.com/ru/post/1765222/


All Articles