How do I calculate the variance of an age group in sql?

3

Hi guys, I hope you can help me, I have tried to get the variance of a "users" table. The field "Age" is not as such, I calculate the age of each user in the following way:

select trunc(months_between(sysdate,FechaNac)/12) edad
from usuarios

Now I'm trying to pass that as an argument to the STDDEV function from sql in oracle:

select STDDEV(edad),(select trunc(months_between(sysdate,FechaNac)/12) edad
from usuarios)
from usuarios

but I get the following error:

SQL state 42000: ORA-00904: "EDAD": invalid identifier

What could it be? greetings

    
asked by Antonio AguIrre 11.10.2016 в 06:33
source

1 answer

2

You have 2 problems.

a) The StdDev function returns the standard deviation, not the variance of a variable. If you want to obtain the variance, use the Variance

B) You use an invalid syntax. Trying to interpret what you want to achieve, I would say that is equivalent to this:

with Edades as (
  select trunc(months_between(sysdate, FechaNac) / 12) edad
    from usuarios
)
select Variance(edad) as VarianzaEdad
  from Edades;
/

that, if you do not want to use a CTE (which in my opinion is much more readable), it would be equivalent to this:

select Variance(trunc(months_between(sysdate, FechaNac) / 12)) as VarianzaEdad
  from usuarios
/ 
    
answered by 11.10.2016 в 12:08