Aggregation Functions 聚合函数 (page 246)
Aggregation functions can operate in analytic mode or conventional non-analytic mode. Aggregation
functions in non-analytic mode reduce the result set to fewer rows. However, in analytic mode,
aggregation functions do not reduce the result set. Further, the aggregation functions can fetch both
aggregated and non-aggregated columns in the same row.
Aggregation functions in analytic mode
provide the ability to aggregate data at different levels without any need for a self-join.
聚合函数能运作于分析模式或者传统的非分析模式。非分析模式的聚合函数将结果集缩减成几行。然而,分析模式,聚合函数不会缩减结果集。再者,聚合函数能在同一行取出聚合和非聚合的列。
分析模式的聚合函数提供了在不同层次聚合数据而不需要自连接的能力。
Analytic functions are useful in writing complex report queries aggregating data at different levels.
Consider a demographic market analysis report for a product, a favorite among advertising executives,
which requires sales data to be aggregated at myriad levels such as age, gender, store, district, region,
and country. Aggregation functions in the analytic mode can be effectively utilized to implement this
market analysis report with ease. Analytic functions will markedly
improve the clarity and performance
of the SQL statements, compared to its non-analytic counterparts.
分析函数对写在不同层次上聚集数据的复杂报告查询是有用的。考虑一款产品的人口统计的市场分析报告,是在广告经理中的最爱,需要在很多层次,诸如年龄,性别,库存,片区,地区以及国家,进行销售数据的聚合。分析模式的聚合函数能轻松,有效的用于执行这种市场分析报告。分析函数显著的
提升了SQL语句的清晰度和性能,相比于非分析模式。
Let’s review the example in the Listing 8-2. The SQL statement is calculating the running of sum of
Sale column from the beginning of the year for a product, country, region, and year combination. The
clause partition by product, country, region, year specifies the partition columns. Within the data
partition, rows are sorted by the Week column using the clause order by week .
我们考察一下列表8-2的例子。SQL语句计算Sale列的从年头起,按产品,国家,区域和年份组合(聚合)的累计和。子句partition by
product, country, region, year 指定分区列。在数据分区中,用子句order by
week使得行按照Week列排序。
In Listing 8-2, the SQL is calculating the running of sum of Sale column, so the analytic function
must operate on window of rows from the beginning of the year to the current week. That goal is
achieved by the windowing clause rows between unbounded preceding and current row.
The sum(sale)
function calculates the sum of Sale column values over this window of rows. Since the rows are sorted by
the Week column, the sum function is operating over a set of rows from the beginning of the year until
the current week.
在列表8-2中,SQL计算在Sale列上的累加和,因此分析函数必须运作于从年头到当前周的行窗口上。通过开窗子句 rows between
unbounded preceding and current row可以完成这个目标。
函数sum(sale)
计算在行窗口上Sale列值的和。由于行按Week列排序,sum函数运作在从年头到当前周的行集上。
Listing 8-2. Running Sum of Sale Column
1 select year, week,sale,
2 sum (sale) over(
3 partition by product, country, region, year
4 order by week
5 rows between unbounded preceding and current row
6 ) running_sum_ytd
7 from sales_fact
8 where country in ('Australia') and product ='Xtend Memory'
9* order by product, country,year, week
/
YEAR WEEK SALE RUNNING_SUM_YTD
----- ---- ---------- ----------------
...
2000 49 42.38 3450.85
2000 50 21.19 3472.04
2000 52 67.45 3539.49
2001 1 92.26 92.26
2001 2 118.38 210.64
2001 3 47.24 257.88
2001 4 256.70 514.58
...
Notice in the output of Listing 8-2, column Running_sum_ytd is the output of the sum function in
the analytic mode. The column value resets at the onset of the new year 2001. Since year is also a
partitioning column, so a new partition starts with each new year.
注意列表8-2的输出,列Running_sum_ytd是sum函数分析模式的输出。列值在新的2001年的开端重置了。因为年也是一分区列,所以对于每一新的年开创一个新的分区。
When a new year begins, the window slides to the next data partition, and the sum function begins
aggregating from Week 1. Implementing this functionality with a conventional SQL statement would
lead multiple self-joins and/or costly
column level sub queries.
当一新的年开始,窗口滑向下一个数据分区,且sum函数从第一周开始聚合。如果使用传统的SQL语句执行这些功能将导致多次自连接和/或昂贵的
列级子查询。
Aggregate Function Over An Entire Partition 在整个分区上的聚合函数
In some cases, analytic functions might need to be applied over all rows in a given data partition. For
example, computing the maximum value of the Sale column for the entire year would require a window
encompassing every row in the data partition. In the Listing 8-3, you use the SQL clause rows between
unbounded preceding and unbounded following
to specify that the MAX function applies to all rows in a
data partition.
The key difference between Listing 8-2 and Listing 8-3 is that the clause unbounded
following specifies the window size to include all rows in a data partition.
在某些情况下,分析函数可能需要应用于给定数据分区的所有行之上。例如,计算Sale列在整年的最大值将需要一包含在数据分区每一行的窗口。在列表8-3
中,你使用SQL子句rows between unbounded preceding and unbounded following
指定MAX函数应用于数据分区的所有行。
列表8-2和8-3的主要不同是子句unbounded
following指定窗口的尺度包含在数据分区上的所有行。
Listing 8-3. Maximum of Sale Column
1 select year, week,sale,
2 max (sale) over(
3 partition by product, country, region ,year
4 order by week
5 rows between unbounded preceding and unbounded following
6 ) Max_sale
7 from sales_fact
8 where country in ('Australia') and product ='Xtend Memory'
9* order by product, country,year, week
/
YEAR WEEK SALE MAX_SALE
----- ---- ---------- ---------------
...
2000 44 135.24 246.74
2000 45 67.62 246.74
2000 46 246.74 246.74
...
2000 50 21.19 246.74
2000 52 67.45 246.74
2001 1 92.26 278.44
2001 2 118.38 278.44
...
Granular Window Specifications 窗口规格的粒度
Window specification can be more granular, too. Let’s say that you want to calculate the Maximum of
Sale column for a five week window period encompassing two weeks prior to the current week, the
current week, and the two weeks following the current week. You can do that using the clause rows
between 2 preceding and 2 following .
窗口的规格能够更加的(小)粒度。我们说你想要计算Sale列的每五周窗口周期,包含当前周的前两周、当前周、当前周的后两周,的最大值。你就能使用子句rows between 2 preceding and 2 following 。
In the Listing 8-4, for week 36, the maximum value for the Sale column in the 5 week window is
178.52. For week 37, the maximum value for the Sale column in the 5 week window is 118.41. You can see those values in the MAX_WEEKS_5 column of the output.
在列表8-4中,对于36周而言,Sale列在5周窗口中的最大值是178.52。对37周而言,Sale列在5周窗口中的最大值是118.41。你可以从MAX_WEEKS_5列中看到这些值的输出。
Listing 8-4. Maximum of Sale Column for a Span of Five Weeks Window
1 select year, week,sale,
2 max (sale) over(
3 partition by product, country, region ,year
4 order by week
5 rows between 2 preceding and 2 following
6 ) max_weeks_5
7 from sales_fact
8 where country in ('Australia') and product ='Xtend Memory'
9* order by product, country,year, week
/
YEAR WEEK SALE MAX_WEEKS_5
---- ---- ---------- -----------
...
2000 34 178.52 178.52
2000 35 78.82 178.52
2000 36 118.41 178.52
2000 37 117.96 118.41
2000 38 79.36 118.41
...
Default Window Specification 默认的窗口规格
The default windowing clause is rows between unbounded preceding and current row.
If you do not
explicitly specify a window, you’ll get the default window. It is a good approach to specify this clause
explicitly to avoid ambiguities.
默认的开窗子句是rows between unbounded preceding and current row。
如果你不明确的指定一个窗口,你将获得默认的窗口。显示的指定这条子句避免歧义是好的(编程)方法。
分享到:
相关推荐
本章“《Pro Oracle SQL》CHAPTER 9 The Model Clause”重点讲解了Model子句的使用,特别是其在聚合操作中的应用。 Model子句的基本结构允许用户指定一个数据区域(即模型区域),在这个区域内对数据进行计算和操作...
Chapter 6 - SQL Functions Chapter 7 - Aggregation and Grouping Chapter 8 - Multi-Table Queries Chapter 9 - Data Transactions Chapter 10 - Advanced Queries and Scripting Chapter 11 - Full-...
Chapter 6 - SQL Functions Chapter 7 - Aggregation and Grouping Chapter 8 - Multi-Table Queries Chapter 9 - Data Transactions Chapter 10 - Advanced Queries and Scripting Chapter 11 - Full-...
Chapter 3 - Tools for Accessing SQL Server Chapter 4 - Introducing Transact-SQL Language Chapter 5 - Data Retrieval Chapter 6 - SQL Functions Chapter 7 - Aggregation and Grouping ...
Chapter 3 - Tools for Accessing SQL Server Chapter 4 - Introducing Transact-SQL Language Chapter 5 - Data Retrieval Chapter 6 - SQL Functions Chapter 7 - Aggregation and Grouping ...
Chapter 3 - Tools for Accessing SQL Server Chapter 4 - Introducing Transact-SQL Language Chapter 5 - Data Retrieval Chapter 6 - SQL Functions Chapter 7 - Aggregation and Grouping ...
标题中的"Recurrent Squeeze-and-Excitation Context Aggregation Net for Single Image Deraining"是一种用于单张图像去雨的深度学习模型,它结合了循环神经网络(RNN)与卷积神经网络(CNN)的优势,旨在提升图像...
Unsupervised Part-based Weighting Aggregation of Deep Convolutional Features for Image Retrieval.pdf
在立体匹配过程中,代价聚合(Cost Aggregation)是一个关键步骤,用于计算像素级别的匹配代价,并通过某种策略进行融合,以减少不匹配的可能性。Segment-Tree是一种数据结构,常用于高效地处理区间或段上的操作,...
在"ns-3-LTE-Carrier-Aggregation-master.zip_CQI-ReportConfig_aggrega"这个压缩包中,包含了NS-3 LTE载波聚合模块的相关代码和配置,这些资源可以帮助我们理解并实现CA中的CQI报告策略。 首先,我们需要了解CQI...
《A Non-Local Cost Aggregation Method for Stereo Matching》是由杨庆雄提出的一种针对立体匹配问题的非局部成本聚合方法。立体匹配是计算机视觉领域中的一个关键任务,它旨在找到两个不同视角图像(左视图和右...
### Oracle SQL Recipes: A Problem-Solution Approach #### Introduction _Oracle SQL Recipes: A Problem-Solution Approach_, authored by Grant Allen, Bob Bryla, and Darl Kuhn, is a comprehensive guide ...
Oracle SQL 是一种强大的数据库查询和管理语言,广泛用于企业级数据管理和分析。在这个文档中,NiCoBee 提供了一组 Oracle SQL 的练习题,旨在帮助学习者掌握各种查询技术。我们将深入探讨其中的一些关键概念。 1. ...
本项目提供的"A Non-Local Cost Aggregation Method for Stereo Matching"完整代码,旨在实现这一先进的方法。 非局部成本聚合的核心思想在于,图像中的每个像素不仅与其直接相邻的像素比较,还与图像中的所有其他...
在C#编程语言中,模块11:聚合、命名空间和高级作用域是理解软件设计与架构的关键部分。本模块深入探讨了内部类、方法和数据的使用,以及如何通过聚合来增强代码的复用性和模块化。同时,它还介绍了命名空间的使用,...
链路聚合,或称LAG(Link Aggregation Group),是一种将多个物理连接组合成一个逻辑链路的技术,使得媒体访问控制(MAC)客户端可以将链路聚合组视为单一的高带宽链接。这种技术的核心是通过N个并行的全双工点对点...
"Cross-Scale Cost Aggregation for Stereo Matching" 是一种先进的立体匹配算法,该算法通过在不同尺度上聚合代价信息来提高匹配的准确性和效率。本项目提供了该算法的C++实现,适用于Visual Studio 2019环境。 在...
Fine-Grained Recognition via Attribute-Guided Attentive Feature Aggregation
在文件"A Non-Local Cost Aggregation Method for Stereo Matching"中,作者可能详细阐述了该方法的理论基础、算法流程、实验设置以及与其他方法的比较。通过实验结果,我们可以预期该方法在运行效果和时间效率上都...