First, the table is as follows:
|
A |
B |
C |
D |
1 |
A |
1 |
2 |
3 |
2 |
B |
1 |
3 |
4 |
3 |
C |
2 |
4 |
5 |
4 |
D |
2 |
5 |
6 |
5 |
E |
3 |
6 |
7 |
Usage of over: Over is used with the aggregate function to generate additional columns. Example: Select T. A, T. B, t.c, sum (c) over (partition by B order by a) sum1 from test T This statement indicates that the columns A, B, and C plus the sum1 column in the test table are sorted by column A and grouped by Column B from column C. The number obtained by continuous addition is placed in the corresponding sum1 column. For example, sum1 = 5 indicates B = 1 and the sum before the second row is 2 + 3 = 5.
|
A |
B |
C |
Sum1 |
1 |
A |
1 |
2 |
2 |
2 |
B |
1 |
3 |
5 |
3 |
C |
2 |
4 |
4 |
4 |
D |
2 |
5 |
9 |
5 |
E |
3 |
6 |
6 |
Sum (c) is a aggregate function. You can also use other functions such as AVG () and count (). Partition specifies which row to group Order specifies the order, which is used in the over function to indicate continuous summary. If this parameter is not added, the result is as follows: Select T. a, T. b, t.c, sum (c) over (partition by B order by T. a) sum1, sum (c) over (partition by B) sum2 from test T
|
A |
B |
C |
Sum1 |
Sum2 |
1 |
A |
1 |
2 |
2 |
5 |
2 |
B |
1 |
3 |
5 |
5 |
3 |
C |
2 |
4 |
4 |
9 |
4 |
D |
2 |
5 |
9 |
9 |
5 |
E |
3 |
6 |
6 |
6 |
Note the differences between sum1 and sum2. Note: 1. If partition by X first and order by Y, the records with the same x Field Values in the same group will be sorted by Y field, and then sum () the role of over () is to accumulate by field a ---- Group Continuous summation 2. If order by Y is not present, sum (a) over () is executed to process the records in the same group after grouping as a record. 3. If neither of them exists, that is, sum (a) over (), and null in parentheses after over, sum (a) over () the field value is the sum of all values in column A, and the sum (a) over () of each record is the same ---- sum. |