简体   繁体   English

SQL Postgres双重分组

[英]Sql Postgres double group by

I've got table with three columns: place_id, date, value. 我有三列的表:place_id,date,value。 For each place for each date there is value, for example 每个日期的每个地方都有价值,例如

place_id: 1, date: '2014-01-01', value: 1 place_id:1,日期:'2014-01-01',值:1
place_id: 2, date: '2014-01-01', value: 2 place_id:2,日期:'2014-01-01',价值:2
place_id: 1, date: '2014-02-02', value: 3 place_id:1,日期:'2014-02-02',价值:3
place_id: 1, date: '2014-02-03', value: 4 place_id:1,日期:'2014-02-03',价值:4
place_id: 2, date: '2014-02-15', value: 5 place_id:2,日期:'2014-02-15',价值:5

My goal is to find for each month sum of max value for all places. 我的目标是找到每个月所有地方的最大值总和。 In the context of data above it should looks like (don't matter how date must be showed like - as two columns year and month or one column 'yyyy-MM-01'): 在上面的数据的上下文中它应该看起来(无论日期必须如何显示 - 如两列年和月或一列'yyyy-MM-01'):

date: '2014-01-01', sum_of_max: 3 日期:'2014-01-01',sum_of_max:3
date: '2014-02-01', sum_of_max: 9 日期:'2014-02-01',sum_of_max:9

As I got, I have to use group by twice - firstly to get maximum value for month and place, secondly to summarize maximums I got on first step. 在我得到的时候,我必须两次使用group - 首先是为了获得月份和地点的最大价值,其次是总结我在第一步获得的最大值。 But what way let me do it with best performance? 但是,什么方法可以让我达到最佳性能呢?

PS If it's matter, I'm using PostgreSql 9.2 PS如果有问题,我正在使用PostgreSql 9.2

I don't see good alternatives to using a subquery. 我看不到使用子查询的替代方法。 The Postgres datetrunc() function can help with grouping values from your base table by month, as it seems you want to do. Postgres datetrunc()函数可以帮助您按月对基表中的值进行分组,就像您想要的那样。

SELECT month, SUM(max_value) AS sum_of_max
FROM (
  SELECT place_id, date_trunc('month', date) AS month, MAX(value) AS max_value
  FROM my_table
  GROUP BY place_id, month
  ) mx
GROUP BY month

One way is a subquery with two group by : 一种方法是子查询,其中包含两个group by

select mm, sum(maxvalue)
from (select place_id, date_trunc('month', date) as mm, max(value) as maxvalue
      from table t
      group by place_id, date_trunc('month', date)
     ) t
group by mm;

Using distinct on in the subquery will probably give better performance: 在子查询中使用distinct on可能会提供更好的性能:

select mm, sum(maxvalue)
from (select distinct on (place_id, date) place_id, date_trunc('month', date) as mm,
             max(value) as maxvalue
      from table t
      order by place_id, date_trunc('month', date), value desc
     ) t
group by mm;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM