使用:
sum(Age) over(partition by Country) as Total
Id FirstName LastName Age Country Total
----------- --------------- ---------- ----------- --------------- -----------
5 Betty Doe 28 UAE 28
6 Fred XXX 1 UK 99731
7 Angela XXX 37 UK 99731
8 Dave XXX 2000 UK 99731
9 Tony YYY 9000 UK 99731
10 Tony ZZZ 323 UK 99731
11 Pete ZZZ 88323 UK 99731
3 David Robinson 22 UK 99731
4 John Reinhardt 25 UK 99731
1 John Doe 31 USA 53
2 Robert Luna 22 USA 53
我得到了我所期望的。这是为了将所有行分为 3 个“帧”(阿联酋、英国和美国),并计算每个帧中的年龄总和。
但是:
sum(Age) over(partition by Country order by LastName) as Total
Id FirstName LastName Age Country Total
----------- --------------- ---------- ----------- --------------- -----------
5 Betty Doe 28 UAE 28
4 John Reinhardt 25 UK 25
3 David Robinson 22 UK 47
6 Fred XXX 1 UK 2085
7 Angela XXX 37 UK 2085
8 Dave XXX 2000 UK 2085
9 Tony YYY 9000 UK 11085
10 Tony ZZZ 323 UK 99731
11 Pete ZZZ 88323 UK 99731
1 John Doe 31 USA 31
2 Robert Luna 22 USA 53
我不完全确定发生了什么。
看起来好像是使用“County”和“LastName”的组合来创建多个框架,所以英国的看起来是
UK,Reinhardt
UK,Robinson
UK,XXX
UK,YYY
UK,ZZZ
然后计算每个帧中的总数。几乎就像具有 2 个参数的 GroupBy,
Group by Country, LastName
,然后在遍历每一行时将它们相加。
如果是这样的话,下面到底发生了什么?为什么它不像上面那样总结每一帧?
sum(Age) over(partition by Country, LastName order by FirstName) as Total
Id FirstName LastName Age Country Total
----------- --------------- ---------- ----------- --------------- -----------
5 Betty Doe 28 UAE 28
4 John Reinhardt 25 UK 25
3 David Robinson 22 UK 22
7 Angela XXX 37 UK 37
8 Dave XXX 2000 UK 2037
6 Fred XXX 1 UK 2038
9 Tony YYY 9000 UK 9000
11 Pete ZZZ 88323 UK 88323
10 Tony ZZZ 323 UK 88646
1 John Doe 31 USA 31
2 Robert Luna 22 USA 22
下面是测试代码
-- create
CREATE TABLE Customer (
Id int,
FirstName varchar(15),
LastName varchar(10),
Age int,
Country varchar(15)
);
-- insert
INSERT INTO Customer(Id,FirstName,LastName,Age,Country) VALUES (1, 'John', 'Doe', 31, 'USA');
INSERT INTO Customer(Id,FirstName,LastName,Age,Country) VALUES (2, 'Robert', 'Luna', 22, 'USA');
INSERT INTO Customer(Id,FirstName,LastName,Age,Country) VALUES (3, 'David', 'Robinson', 22, 'UK');
INSERT INTO Customer(Id,FirstName,LastName,Age,Country) VALUES (4, 'John', 'Reinhardt', 25, 'UK');
INSERT INTO Customer(Id,FirstName,LastName,Age,Country) VALUES (5, 'Betty', 'Doe', 28, 'UAE');
INSERT INTO Customer(Id,FirstName,LastName,Age,Country) VALUES (6, 'Fred', 'XXX', 1, 'UK');
INSERT INTO Customer(Id,FirstName,LastName,Age,Country) VALUES (7, 'Angela', 'XXX', 37, 'UK');
INSERT INTO Customer(Id,FirstName,LastName,Age,Country) VALUES (8, 'Dave', 'XXX', 2000, 'UK');
INSERT INTO Customer(Id,FirstName,LastName,Age,Country) VALUES (9, 'Tony', 'YYY', 9000, 'UK');
INSERT INTO Customer(Id,FirstName,LastName,Age,Country) VALUES (10, 'Tony', 'ZZZ', 323, 'UK');
INSERT INTO Customer(Id,FirstName,LastName,Age,Country) VALUES (11, 'Pete', 'ZZZ', 88323, 'UK');
SELECT
Id,
FirstName,
LastName,
Age,
Country,
sum(Age) over(partition by Country) as Total
--sum(Age) over(partition by Country order by LastName) as Total
--sum(Age) over(partition by Country, LastName order by FirstName) as Total
FROM Customer
添加
order by LastName
返回的不是单独的帧,而是直到(并包括)当前行的所有行的累积总和。
使用
partition by Country, LastName order by FirstName
,您可以为每个国家/地区、姓氏组合创建一个框架,并返回该框架中内的累积总和。