0

我需要将数据聚合到应用程序并找到数字列的最小值、最大值、平均值

是)我有的

  Application  income   score_1
    ax          800        77
    ax          900        72
    ax          700        62    
    ax          600        55    

我需要的

  Application  min(income) max(income)    avg(income)  min(score_1) max(score_1)    avg(score_1)
    ax          800           900              750        62           77           224.75         

我可以写一个查询

select min(income),max(income),avg(income),min(score_1),max(score_1),avg(score_1)
from table name group by application; --IT WORKS..!!

但在表中,我有 20 个数字列,我需要将这些列的最小值、最大值、平均值的统计信息放入表中。有没有什么方法可以完成这项工作,而不是手动编写列名来获取 avg,min ,max

4

1 回答 1

3

它们都是数字,所以你可以这样做:

select application, which,
       min(val), max(val), avg(val)
from t, lateral
     (values ('income', income), ('score_1', score_1)) v(which, val)
group by application, which;

这会将值放在每列的单独行中。

是一个重新测试者。

于 2018-07-14T00:09:18.727 回答