Finding mean, standard deviation ,percentiles for all numeric variables in table

user8545255 picture user8545255 · May 21, 2018 · Viewed 7k times · Source

I have 30 numeric numeric columns in a table .I want to find mean,std, percentiles for all the columns in table.I don't want to write all the column names manually like below

select date,
      avg(col1), stddev(col1),
      avg(col2), stddev(col2), 
from table name group by date;

Is there any way to find mean, std, percentiles for all the columns at once.

Answer

Gordon Linoff picture Gordon Linoff · May 21, 2018

You can simplify the logic using a lateral join:

select which, min(val), max(val), stddev(val), avg(val)
from t, lateral
     (values ('col1', col1), ('col2', col2), . . . 
     ) v(which, val)
group by which;

You still have to list the columns, but you only need to do so once in the values clause.