标签:
Over 窗口函数,能够在Select 子句中,对查询的结果集进行“滑动-聚合”运算;如果使用count,那么基于滑动窗口的聚合语义同 base+1 累加;如果使用sum,那么基于滑动窗口的聚合语义等同于数据累加。
Over()子句的运算顺序在Select 子句之后,在Order By子句之前。
滑动窗口计算原理:窗口的大小是由Over 的Partition By子句界定,窗口滑动的顺序是由Over的Order by子句指定。在计算聚合值时,使用<=(Order by Asc)或 >=(Order by Desc)计算滑动窗口的聚合值,某一个窗口的计算逻辑类似于
select t.*, AggregationFuncation() Over(partition by t.ColumnName_Partition order by t.ColumnName_Order asc) from dbo.TableName t select AggregationFuncation from dbo.TableName t where t.ColumnName_Partition=Over_PartitionColumnValue and t.ColumnName_Order<=Over_OrderColumnValue
1,创建示例数据
--create table create table dbo.dt_test ( ID int, Code int ) go --insert data insert into dbo.dt_test(ID,Code) values(3,1),(3,2),(1,1),(1,2),(2,3),(1,2) go
2,测试Over 创建函数的 滑动-聚合
--test over select ID,Code, count(0) over(partition by Code order by ID) as Count_Over, sum(ID) over(partition by Code order by ID) as Sum_Over from dbo.dt_test
查询的结果按照 Code 排序,在Over 函数中,按照Code分区,按照ID排序。
分析查询结果:
2.1当Code=1时,在这个分区(窗口)中,有两行,ID分别是1,3
当Code=1,ID=1时,是分区的第一行,Count_Over=1,Sum_Over=1,这是滑动窗口的第一行,聚合值是的计算逻辑是
select count(0) as Count_Over, sum(ID) as Sum_Over from dbo.dt_test where Code=1 and ID<=1
当Code=1,ID=3时,是分区的第二行,Count_Over=2,Sum_Over=4,这是滑动窗口的第二行,聚合值是的计算逻辑是
select count(0) as Count_Over,
sum(ID) as Sum_Over
from dbo.dt_test
where Code=1
and ID<=3
2.2当Code=2时,在这个分区中,有三种,ID分别是1,1,3
当Code=2,ID=1时,是分区的第一行,Count_Over=2,Sum_Over=2,这是滑动窗口的第一行,聚合值是的计算逻辑是
select count(0) as Count_Over,
sum(ID) as Sum_Over
from dbo.dt_test
where Code=2
and ID<=1
当Code=2,ID=1时,是分区的第二行,Count_Over=2,Sum_Over=2,这是滑动窗口的第二行,聚合值是的计算逻辑是
select count(0) as Count_Over,
sum(ID) as Sum_Over
from dbo.dt_test
where Code=2
and ID<=1
当Code=2,ID=3时,是分区的第三行,Count_Over=3,Sum_Over=5,这是滑动窗口的第三行,聚合值是的计算逻辑是
select count(0) as Count_Over,
sum(ID) as Sum_Over
from dbo.dt_test
where Code=2
and ID<=3
参考文档:
http://www.cnblogs.com/biwork/p/3244527.html
https://msdn.microsoft.com/zh-cn/library/ms189461.aspx
http://www.cnblogs.com/jeffwongishandsome/archive/2010/12/04/1896672.html
http://www.cnblogs.com/CareySon/p/3411176.html
标签:
原文地址:http://www.cnblogs.com/ljhdo/p/4533456.html