在postgresql中获取源数据和聚合数据

时间:2017-07-24 09:42:57

标签: sql postgresql

我有以下查询:

SELECT round(cast(estimated_memusage as numeric), 2) as x,
       memusage as y
FROM measures_predictions

我还想获得每列的最小值和最大值。

有没有办法构建一个返回上面第一个选择的查询,而min,max,而不必对相同的数据(一个正常,一个分组)运行两个选择,是否可以临时存储数据,制作小组,并在结尾发送两个?

2 个答案:

答案 0 :(得分:0)

您可以使用以下方法

SELECT 
    round(cast(estimated_memusage as numeric),2) as x
  , memusage as y
  , min(ColumnName) over(partition by Columns) as Min 
  , max(ColumnName) over(partition by Columns) as Max FROM measures_predictions

答案 1 :(得分:0)

Window functions

select
    round(cast(estimated_memusage as numeric), 2) as x,
    memusage as y,
    min(estimated_memusage) over() as min_estimated_memusage,
    min(memusage) over () as min_memusage,
    max(estimated_memusage) over() as max_estimated_memusage,
    max(memusage) over () as max_memusage
from measures_predictions