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.