It Ninja Turtles Oracle Row-to-column, column-changing career

Source: Internet
Author: User

First, row to column

The following format is required

Convert to:

This is the most common row-to-column, the main principle is to use the Decode function, aggregation function (sum), combined with the group by group implementation of the

[SQL]View Plaincopy
  1. Create Table Test (
  2. ID VARCHAR2 (255) primary key not null,
  3. name varchar2 (255),
  4. Course VARCHAR2 (255),
  5. Score VARCHAR2 (255)
  6. );
  7. Insert into test values(Sys_guid (),' Zhangsan ', ' Chinese '  , 85);
  8. Insert into test values(Sys_guid (),' Zhangsan ',' math '   , 78);
  9. Insert into test values(Sys_guid (),' Zhangsan ',   ' English ', 90);
  10. Insert into test values(Sys_guid (),' Lisi ', ' Chinese '  , 73);
  11. Insert into test values(Sys_guid (),' Lisi ',' math '  , 84);
  12. Insert into test values(Sys_guid (),' Lisi ', ' English '  , 92);

The row-to-column SQL statement is:

[SQL]View Plaincopy
  1. Select T. name ,   
  2. sum (Decode (t.course, ' language ', score,null)) as Chinese,
  3. sum (Decode (t.course, ' math ', score,null)) as Math,
  4. sum (Decode (t.course, ' English ', score,null)) as 中文版
  5. from Test T
  6. Group by T. name    
  7. Order by T. name    


Second, the list of career change

The following format

Converted to

This is the most common column change, the main principle is to use the union inside the SQL

[SQL]View Plaincopy
  1. Create Table Test (
  2. ID VARCHAR2 (255) primary key not null,
  3. name varchar2 (255),
  4. Ch_score varchar2 (255),
  5. Math_score varchar2 (255),
  6. En_score VARCHAR2 (255)
  7. );
  8. Insert   into test values(Sys_guid (),' Zhangsan ', 88,76,90);
  9. Insert into test values(Sys_guid (),' Lisi ',  91,67,82);

The column career SQL statement is:

[SQL]View Plaincopy
  1. Select name, ' language ' COURSE, ch_score as score from Test
  2. Union Select name, ' math ' COURSE, Math_score as score from Test
  3. Union  Select name, ' English ' COURSE, en_score as Score from test
  4. Order by name , COURSE

It Ninja Turtles Oracle Row-to-column, column-changing career

Related Article

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.