Skip to content
Advertisement

A function calculating average age in years in SQL [postgresql]

I am trying to create a plpgsql function that calculates the average age (in years) of some persons with ids (integers) stored in another table.

The code is:

Of course, dateofbirth is of type date. My problem is that the table ages that is created does not contain anything and thus I cannot return the correct result (the average age of its column). The function’s return type is integer (and “Returns set” is not selected in pgadmin).

I am using PostgreSQL 9.3.4, pgAdmin III version 1.18.1.

Thank you.

Advertisement

Answer

If you are looking for average age from a person table you can do it much simpler. Something like this for starters:

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement