Oracle column forwarding

Source: Internet
Author: User
1. create a table named TEST 2. add data INSERTINTOTEST (STUDENT, COURSE, SCORE) to the TEST table select Zhang San, Chinese, Zhang San, mathematics, 87fromdualunionselect Zhang San, English, 82fromdualunionselect Zhang San, physics, 90 fromdualunio

1. create a table named TEST 2. add data insert into test (STUDENT, COURSE, SCORE) to the TEST table 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 unio

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 the sum. For example, if there are two three records in the record, the result displayed in the column-to-row is the sum of the two three results.

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.