将查询的行分组在一起

时间:2018-06-09 00:29:23

标签: postgresql date grouping window-functions postgresql-9.5

我需要将查询的行组合在一起以创建行的bin(组)。应该将组的范围动态地传递给查询(例如,从变量@b = [4])。

我开始使用row_number()函数来获取按顺序编号的行。但我不得不考虑如何继续下去。这是我的剧本:

select date, measure, row_number() over (order by date,measure)
from dwh.overview_all_data

这是我查询的结果:

date  measure  row_number
1998-01-11  AOX  1
1998-01-11  Ammonium  2
1998-01-11  Arsenic  3
1998-01-11  Atrazine  4
1998-01-11  Biochemical Oxygen  5
1998-01-11  Cadmium  6
1998-01-11  Calcium  7
1998-01-11  Cesium  8
1998-01-11  Chemical Oxygen Demand (Cr)  9
1998-01-11  Chemical Oxygen Demand (Mn)  10
1998-01-11  Chlorides  11
1998-01-11  Chromium  12
1998-01-11  Copper  13
1998-01-11  Dissolved oxygen  14
1998-01-11  Fecal coliforms  15
1998-01-11  Iron  16

让我们说我希望将这16行放入4个箱子(b = [4]),我还需要定义每个箱子(组)的开始和结束日期。即结果如下:

date  measure  row_number  bin, startdate  enddate
1998-01-11  AOX  1  1  1998-01-11  1998-01-11
1998-01-11  Ammonium  2  1  1998-01-11  1998-01-11
1998-01-11  Arsenic  3  1  1998-01-11  1998-01-11
1998-01-11  Atrazine  4  1  1998-01-11  1998-01-11
1998-01-11  Biochemical Oxygen  5  2  1998-01-11  1998-01-20
1998-01-15  Cadmium  6  2  1998-01-11  1998-01-20
1998-01-15  Calcium  7  2  1998-01-11  1998-01-20
1998-01-20  Cesium  8  2  1998-01-11  1998-01-20
1999-01-21  Chemical Oxygen Demand (Cr)  9  3  1999-01-21  2005-01-22
1999-01-22  Chemical Oxygen Demand (Mn)  10  3  1999-01-21  2005-01-22
1999-01-22  Chlorides  11  3  1999-01-21  2005-01-22
2005-01-22  Chromium  12  3  1999-01-21  2005-01-22
2005-02-01  Copper  13  4  2005-02-01  2007-04-01
2005-02-11  Dissolved oxygen  14  4  2005-02-01  2007-04-01
2005-03-15  Fecal coliforms  15  4  2005-02-01  2007-04-01
2007-04-01  Iron  16  4  2005-02-01  2007-04-01

1 个答案:

答案 0 :(得分:0)

我能搞清楚:

Select t1.measure, t1.bin, Min(t1.date) startDate, Max(t1.date) EndDate, count(*)
from(
select date, measure, row_number() over (order by date,measure), floor(((row_number() over (order by date,measure))-1) /88) as bin
from dwh.overview_all_data
  order by date, measure
) t1
group by t1.measure, t1.bin
order by t1.measure, t1.bin, startDate, EndDate

bin列中的数字“88”分别计算如下:

select 8819 / 100 -- = 88

“100”是我们想要的箱子(组)的数量。它由用户提供。

“8819”是内部语句的行数,可以使用此脚本返回:

select count(*) from
    (
      select date, measure, row_number() over (order by date, measure )
      from dwh.overview_all_data
      where date between '1998-01-01' and '2000-01-01'
      order by date, measure
    ) t

通过除以这两个数字,我们得到每个箱子中的行数。