Postgres:一次选择即可获得最小值,最大值,合计值 [英] Postgres: get min, max, aggregate values in one select

查看:117
本文介绍了Postgres:一次选择即可获得最小值,最大值,合计值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Postgresql 8.4。我有一个这样的表:

I am using Postgresql 8.4. I have a table like this:

 type | value
------+-------
 1    | 5
 2    | 6
 1    | 4
 3    | 10

我想写一个选择,使我的&最大值,并且所有类型的总和为 integer [] 。期望的结果应该是:

I want to write one select that will give me the minimum & maximum value, and an aggregate of all the types as integer[]. The desired result should be:

 min | max | types
-----+-----+-----------
 4   | 10  | {1, 2, 3}

要获得最小值和最大值,我已经有:

To get the min and max, I already have:

SELECT MIN(value) min, MAX(value) max FROM table;

要获得独立选择的类型,我使用:

To get the types in a standalone select, I use:

SELECT array_agg(DISTINCT type) types FROM table;

如何将它们组合成一个选择(效率不是太低)?

How can I combine these into one select (that is not too inefficient)?

推荐答案

SELECT array_agg(DISTINCT type) AS types,
       MIN(value) AS min,
       MAX(value) AS max
FROM your_table

这篇关于Postgres:一次选择即可获得最小值,最大值,合计值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆