【问题标题】:get source and aggregated data in postgresql在 postgresql 中获取源数据和聚合数据
【发布时间】:2017-12-29 20:10:20
【问题描述】:

我有以下查询:

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

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

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

【问题讨论】:

    标签: sql postgresql


    【解决方案1】:

    你可以使用下面的方法

    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
    

    【讨论】:

      【解决方案2】:

      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
      

      【讨论】:

        猜你喜欢
        • 2021-03-21
        • 1970-01-01
        • 1970-01-01
        • 2010-10-30
        • 2020-03-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-01
        相关资源
        最近更新 更多