Sum over analysis function usage

Source: Internet
Author: User

 

Sum over analysis function usage

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.

From: http://ljx198308.blog.163.com/blog/static/176274246201011935246547? Fromdm & fromsearch & isfromsearchengine = Yes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.