PL/SQL converts rows and columns

Source: Internet
Author: User

This blog post describes the simple conversion of rows and columns, which is provided in a specific example.

Some people have asked related questions on the Forum before. Many of the answers mentioned above are feasible. Of course, they think that they are very high-end, but they have no answer. Next, go to the topic.

Part1: column-to-row. Requirements:

The following example shows a table:

The table is defined as follows:

create table TEST_TB_GRADE (   ID        NUMBER(10) not null,   USER_NAME VARCHAR2(20 CHAR),   COURSE    VARCHAR2(20 CHAR),   SCORE     FLOAT ) 

Required to be converted:

A feasible PL/SQL statement is provided below:

Select T. user_name as name, sum (decode (T. course, 'China', score, null) as language, sum (decode (T. course, 'mat', score, null) as mathematics, sum (decode (T. course, 'English ', score, null) as English from test_tb_grade T group by T. user_name order by T. user_name

 

The implementation result is shown in the requirement.

Part2: Convert rows to columns. Requirements:

The following example shows a table.

The table structure is as follows:

create table TEST_TB_GRADE2 (   ID         NUMBER(10) not null,   USER_NAME  VARCHAR2(20 CHAR),   CN_SCORE   FLOAT,   MATH_SCORE FLOAT,   EN_SCORE   FLOAT ) 

It must be converted to the following format:

An executable PL/SQL statement is provided below:

Select user_name, 'China' as course, cn_score as score from test_tb_grade2 Union allselect user_name, 'mate' as course, math_score as score from test_tb_grade2 Union allselect user_name, 'English 'as course, en_score as score from test_tb_grade2 order by user_name, Course

 

The implementation result is shown in the requirement.

 

The purpose of this article is to give a specific example of a column-and-column conversion solution in Oracle. There is nothing to be circled. You are welcome to provide other implementations, and you are also welcome to criticize and correct them!

[Click "Green Channel" ----- "follow debuglzq" below to share progress ~]

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.