1. Create a table named Test
2. Add data to the test table
Insert into test (student, course, score)
Select 'zhang san', 'China', 78 from dual Union
Select 'zhang san', 'mat', 87 from dual Union
Select 'zhang san', 'English ', 82 from dual Union
Select 'zhang san', 'Physical ', 90 from dual Union
Select 'Li si', 'China', 65 from dual Union
Select 'Li si', 'mat', 77 from dual Union
Select 'Li si', 'English ', 65 from dual Union
Select 'lily', 'Physical ', 85 from dual
The table data is as follows:
3. Column-to-row
Method · 1:
Select
Student,
Sum (decode (course, 'mat', score) mathematics,
Sum (decode (course, 'Physical ', score) physical,
Sum (decode (course, 'English ', score) English,
Sum (decode (course, 'China', score) Language
From
Test
Group by student
Method · 2:
Select
Student,
Sum (Case course when 'mate' then score else null end) math,
Sum (Case course when 'physical 'Then score else null end) physical,
Sum (Case course when 'then score else null end) English,
Sum (Case course when 'China' then score else null end) Language
From
Test
Group by student
The effect is as follows:
Note: sum indicates sum. For example, there are two three parts in the record, the result displayed in a column-to-row operation is the sum of two three results.