Why does the aggregate function use sum.

Source: Internet
Author: User

Q:

Create Table Test (ID int, name varchar (20), quarter int, profile INT)
Insert into test values (1, 'A', 1,1000)
Insert into test values (1, 'A', 2,2000)
Insert into test values (1, 'A', 3,4000)
Insert into test values (1, 'A', 4,5000)
Insert into test values (2, 'B', 1,3000)
Insert into test values (2, 'B', 2,3500)
Insert into test values (2, 'B', 3,4200)
Insert into test values (2, 'B', 4,5500)

Select * from test

Select ID, name,
[1] as "first quarter ",
[2] as "Quarter 2 ",
[3] as "three quarters ",
[4] as "fourth quarter"
From
Test
Bytes
(
Sum (profile)
For quarter in
([1], [2], [3], [4])
)
As PVT

I would like to ask you why the aggregate function uses sum in this section of SQL code. Isn't it a sum?

Answer: (you must read the following red text carefully, and the answer is here)

 

Create Table Test (ID int, name varchar (20), quarter int, profile INT)
Insert into test values (1, 'A', 1,1000)
Insert into test values (1, 'A', 2,2000)
Insert into test values (1, 'A',) -- here
Insert into test values (1, 'A', 3,4000)
Insert into test values (1, 'A', 4,5000)
Insert into test values (2, 'B', 1,3000)
Insert into test values (2, 'B', 2,3500)
Insert into test values (2, 'B', 3,4200)
Insert into test values (2, 'B', 4,5500)

 

Select * from test

 

Select ID, name,
[1] as "first quarter ",
[2] as "Quarter 2 ",
[3] as "three quarters ",
[4] as "fourth quarter"
From
Test
Bytes
(
Sum (profile)
For quarter in
([1], [2], [3], [4])
)
As PVT

 

/*
ID name: first quarter, second quarter, third quarter, fourth quarter
---------------------------------------------------------------------------
1 A 1000 7000 7000 (4000 summarized here) 5000
2 B 3000 3500 4200 5500 */

 

By the way: Rule syntax pivot ( Aggregate functions (column names) for  Converted column name in (Name of the column to be displayed) ) kHere, the aggregate function can make sum or Max. Do you understand? If you don't understand it, you don't need to search any more. Take a closer look. Here is what you are looking.

 

Why does the aggregate function use sum.

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.