Oracle row-to-column summary and oracle column-to-column Summary

Source: Internet
Author: User

Oracle row-to-column summary and oracle column-to-column Summary

Recently, I have applied row-to-column conversion in my work. Here I will make a simple summary.


The conversion process is as follows:

1. Create a table structure

CREATE TABLE RowToCol (   ID NUMBER(10) not null,   USER_NAME VARCHAR2(20 CHAR),   COURSE VARCHAR2(20 CHAR),   SCORE VARCHAR2(20 CHAR)) 

2. Insert test data (raw data)



3. Implement row-to-column Conversion


4. Specific implementation

The main principle is to use the decode function, aggregate function (sum/max/min/avg), and group by function. The specific SQL statement is as follows:


Select t. user_name as name, MAX (decode (t. course, 'China', score, null) as language, MAX (decode (t. course, 'mat', score, null) as mathematics, MAX (decode (t. course, 'English ', score, null) as English from RowToCol t group by t. user_name order by t. user_name



* If the score column in the example is a numeric value, the aggregate function can use sum/max/min/avg, but only max/min can be used for character rows.




Certificate ------------------------------------------------------------------------------------------------------------------------------------------------------


Note:

Decode function:

It is a unique function compute method, which is equivalent to if... else...

Decode (condition, value 1, translation value 1, value 2, translation value 2,... value n, translation value n, default value)

It is understood:

If (condition = value 1) then return (translation value 1) elsif (condition = value 2) then return (translation value 2 )...... elsif (condition = value n) then return (translation value n) else return (default value) end if


Row-to-column conversion includes six situations: Row-to-column conversion, column-to-row conversion, multi-column conversion to String Conversion, String Conversion to multiple rows, and String Conversion to multiple columns, if you are interested, you can perform further research.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.