SQL: how to calculate the average value from a query

I have been using SQL for about a week in my first full task, and I am trying to compute some statistics from a query where I have combined columns from separate tables.

In particular, I am trying to calculate the average value from a combo table where I had filters applied (or restrictions? I don’t understand SQL ling).

From a study on Google, I learned how to calculate the average:

SELECT AVG(column_name) 
FROM table_name

The problem I am facing is that this seems to work only with existing tables in the database, and not with the new queries I requested.

A simplified version of my code is as follows:

SELECT 
    Animal_Facts.Animal_Name, Animal_Facts.Prev_Reg_Amount, 
    Names.Given_Name, Animal_Class.Class_Description
FROM 
    Names
INNER JOIN
    Animal_Facts ON Names.Name_Key = Animal_Facts.Name_Key
INNER JOIN 
    Animal_Class ON Animal_Facts.Class_Key = Animal_Class.Class_Key

, Class_Description , , , .., Pre_Reg_Amount - .

, , :

AND Animal_Class.Class_Description LIKE ('%pensioner%')

, , :

SELECT AVG(Animal_Facts.Prev_Reg_Amount) from Animal_Facts

, :

SELECT 
    Animal_Facts.Animal_Name, Animal_Facts.Prev_Reg_Amount, 
    Names.Given_Name, Animal_Class.Class_Description
FROM 
    Names
INNER JOIN
    Animal_Facts ON Names.Name_Key = Animal_Facts.Name_Key
INNER JOIN
    Animal_Class ON Animal_Facts.Class_Key = Animal_Class.Class_Key
                 AND Animal_Class.Class_Description LIKE ('%pensioner%')

SELECT AVG(Animal_Facts.Prev_Reg_Amount) 
FROM Animal_Facts

, Excel , . ( ) SQL?

.. , Excel, . , SQL.

+4
1
SELECT AVG(af.Prev_Reg_Amount)
FROM
    Animal_Facts af
    INNER JOIN Animal_Class ac
    ON af.Class_Key = ac.Class_Key
    AND Class_Description LIKE ('%pensioner%')
+6

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


All Articles