Two simple row and column transpose
1, the row and column conversion of fixed number
Such as
Student Subject Grade
--------- ---------- --------
Student1 Language 80
Student1 Math 70
Student1 English 60
Student2 Language 90
Student2 Math 80
Student2 English 100
......
Converted to
English for Chinese mathematics
STUDENT1 80 70 60
STUDENT2 90 80 100
......
The statement is as follows: Select student,
SUM (decode (subject, ' Language ', grade, null)) "Language",
SUM (decode (subject, ' Math ', grade, NULL)) "Mathematics",
SUM (decode (subject, ' English ', grade, null)) "English"
From table
Group by student;
2. Indefinite column and row conversion
Such as
C1 C2
--- -----------
1 I
1 is
1 Who
2 know
2-Way
3 does not
......
Converted to
1 Who am I
2 know
3 does not
This type of conversion must be done with the help of Pl/sql, which gives an example of CREATE OR REPLACE FUNCTION get_c2 (tmp_c1 number)
Return VARCHAR2
Is
COL_C2 VARCHAR2 (4000);
BEGIN
For cur in (SELECT C2 from t WHERE c1 = TMP_C1) loop
COL_C2: = COL_C2 | | CUR.C2;
End LOOP;
COL_C2: = RTrim (COL_C2, 1);
return COL_C2;
End;
/
SQL > select DISTINCT C1, GET_C2 (c1) CC2 from table;