将查询的行分组在一起
Posted
技术标签:
【中文标题】将查询的行分组在一起【英文标题】:Grouping rows of a query together 【发布时间】:2018-06-09 00:29:22 【问题描述】:我需要将查询的行组合在一起以创建行的箱(组)。组的范围应动态传递给查询(例如,从变量@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
【问题讨论】:
除以 4 并使用整数除法或使用floor()
函数删除余数。
它有效,但我必须用 -1 减去 row_number 才能使第一个 bin 和最后一个 bin 正确。你对开始和结束日期有什么想法吗?
【参考方案1】:
我能猜出来:
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
通过将这两个数字相除,我们得到每个 bin 中的行数。
【讨论】:
以上是关于将查询的行分组在一起的主要内容,如果未能解决你的问题,请参考以下文章
SQL中的Group By的查询过程多列分组的查询过程是怎样的?