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.