Statement of Oracle row-to-column Conversion

Source: Internet
Author: User
Tags rtrim

Statement of Oracle row-to-column conversion (posting ):

Row-to-Column

Create Table t_row_col
Select ID, 'c1 'cn, C1 CV
From t_col_row
Union all
Select ID, 'c2 'cn, C2 CV
From t_col_row
Union all
Select ID, 'c3' CN, C3 CV from t_col_row;

Select * From t_row_col order by 1, 2;

1) Aggregate Function
Applicability: 8i, 9i, 10g and later
Select ID,
Max (decode (CN, 'c1', CV, null) as C1,
Max (decode (CN, 'c2 ', CV, null) as C2,
Max (decode (CN, 'c3', CV, null) as C3
From t_row_col
Group by ID
Order by 1;

The max aggregate function can also be replaced by sum, Min, AVG, and other Aggregate functions.

The specified transpose column can only have one column, but the fixed column can have multiple columns. See the following example:

Select Mgr, deptno, empno, ename from EMP order by 1, 2;

Select Mgr,
Deptno,
Max (decode (empno, '20140901', ename, null) "7788 ",
Max (decode (empno, '20140901', ename, null) "7902 ",
Max (decode (empno, '20140901', ename, null) "7844 ",
Max (decode (empno, '20140901', ename, null) "7521 ",
Max (decode (empno, '20140901', ename, null) "7900 ",
Max (decode (empno, '20140901', ename, null) "7499 ",
Max (decode (empno, '20140901', ename, null) "7654"
From EMP
Where Mgr in (7566,769 8)
And deptno in (20, 30)
Group by Mgr, deptno
Order by 1, 2;

Here, the transposed column is empno, and the fixed column is MGR and deptno.

Another way to convert rows to columns is to change the row values in the same group to a single column value, but the transposed row values are not changed to column names:

Id cn_1 cv_1 cn_2 cv_2 cn_3 cv_3
1 C1 V11 C2 V21 C3 v31
2 C1 V12 C2 v22 C3 
3 C1 v13 C2 C3 V33
4 C1 C2 V24 C3 v34
5 C1 V15 C2 C3  
6 C1 C2 C3 v35
7 C1 C2 C3

In this case, you can use the analysis function:

Select ID,
Max (decode (RN, 1, CN, null) cn_1,
Max (decode (RN, 1, CV, null) cv_1,
Max (decode (RN, 2, CN, null) cn_2,
Max (decode (RN, 2, CV, null) cv_2,
Max (decode (RN, 3, CN, null) cn_3,
Max (decode (RN, 3, CV, null) cv_3
From (select ID,
CN,
CV,
Row_number () over (partition by ID order by CN, CV) Rn
From t_row_col)
Group by ID;

2) PL/SQL
Applicability: 8i, 9i, 10g and later 
This can be used when the row value is not fixed.
The following is a package I wrote.
P_rows_column_real is used to convert the first unlimited column mentioned above;
P_rows_column is used to convert the second unlimited column.

Create or replace package pkg_dynamic_rows_column
Type refc is ref cursor;

Procedure p_print_ SQL (p_txt varchar2 );

Function f_split_str (p_str varchar2, p_division varchar2, p_seq INT)
Return varchar2;

Procedure p_rows_column (p_table in varchar2,
P_keep_cols in varchar2,
P_effect_cols in varchar2,
P_where in varchar2 default null,
P_refc in out refc );

Procedure p_rows_column_real (p_table in varchar2,
P_keep_cols in varchar2,
P_effect_col in varchar2,
P_1_t_val in varchar2,
P_where in varchar2 default null,
P_refc in out refc );
End;
/
Create or replace package body pkg_dynamic_rows_column

Procedure p_print_ SQL (p_txt varchar2) is
V_len int;
Begin
V_len: = length (p_txt );
For I in 1 .. v_len/250 + 1 Loop
Dbms_output.put_line (substrb (p_txt, (I-1) * 250 + 1,250 ));
End loop;
End;

Function f_split_str (p_str varchar2, p_division varchar2, p_seq INT)
Return varchar2 is
V_first int;
V_last int;
Begin
If p_seq <1 then
Return NULL;
End if;
If p_seq = 1 then
If instr (p_str, p_division, 1, p_seq) = 0 then
Return p_str;
Else
Return substr (p_str, 1, instr (p_str, p_division, 1)-1 );
End if;
Else
V_first: = instr (p_str, p_division, 1, p_seq-1 );
V_last: = instr (p_str, p_division, 1, p_seq );
If (v_last = 0) then
If (v_first> 0) then
Return substr (p_str, v_first + 1 );
Else
Return NULL;
End if;
Else
Return substr (p_str, v_first + 1, v_last-v_first-1 );
End if;
End if;
End f_split_str;

Procedure p_rows_column (p_table in varchar2,
P_keep_cols in varchar2,
P_effect_cols in varchar2,
P_where in varchar2 default null,
P_refc in out refc) is
V_ SQL varchar2 (4000 );
Type v_keep_ind_by is table of varchar2 (4000) index by binary_integer;
V_keep v_keep_ind_by;

Type v_effect_ind_by is table of varchar2 (4000) index by binary_integer;
V_1_v_1_t_ind_by;

V_keep_cnt int;
V_effect_cnt int;
V_max_cols int;
V_partition varchar2 (4000 );
V_partition1 varchar2 (4000 );
V_partition2 varchar2 (4000 );
Begin
V_keep_cnt: = length (p_keep_cols)-length (replace (p_keep_cols, ',') + 1;
V_1_t_cnt: = length (p_1_t_cols )-
Length (replace (p_1_t_cols, ',') + 1;
For I in 1 .. v_keep_cnt Loop
V_keep (I): = f_split_str (p_keep_cols, ',', I );
End loop;
For J in 1 .. v_rjt_cnt Loop
V_reverse (j): = f_split_str (p_effect_cols, ',', J );
End loop;
V_ SQL: = 'select max (count (*) from '| p_table | 'group ';
For I in 1 .. v_keep.last Loop
V_ SQL: = v_ SQL | v_keep (I) | ',';
End loop;
V_ SQL: = rtrim (v_ SQL ,',');
Execute immediate v_ SQL
Into v_max_cols;
V_partition: = 'select ';
For X in 1 .. v_keep.count Loop
V_partition1: = v_partition1 | v_keep (x) | ',';
End loop;
For Y in 1 .. v_20.t.count Loop
V_partition2: = v_partition2 | v_1_( y) | ',';
End loop;
V_partition1: = rtrim (v_partition1 ,',');
V_partition2: = rtrim (v_partition2 ,',');
V_partition: = v_partition | v_partition1 | ',' | v_partition2 |
', Row_number () over (partition by' | v_partition1 |
'ORDER BY' | v_partition2 | ') Rn from' | p_table;
V_partition: = rtrim (v_partition ,',');
V_ SQL: = 'select ';
For I in 1 .. v_keep.count Loop
V_ SQL: = v_ SQL | v_keep (I) | ',';
End loop;
For I in 1 .. v_max_cols Loop
For J in 1 .. v_20.t.count Loop
V_ SQL: = v_ SQL | 'max (decode (RN, '| I |', '| v_1_( J) |
', Null)' | v_1_( J) | '_' | I | ',';
End loop;
End loop;
If p_where is not null then
V_ SQL: = rtrim (v_ SQL, ',') | 'from ('| v_partition | ''|
P_where | ') group ';
Else
V_ SQL: = rtrim (v_ SQL, ',') | 'from ('| v_partition |
') Group ';
End if;
For I in 1 .. v_keep.count Loop
V_ SQL: = v_ SQL | v_keep (I) | ',';
End loop;
V_ SQL: = rtrim (v_ SQL ,',');
P_print_ SQL (v_ SQL );
Open p_refc for v_ SQL;
Exception
When others then
Open p_refc
Select 'x' from dual where 0 = 1;
End;

Procedure p_rows_column_real (p_table in varchar2,
P_keep_cols in varchar2,
P_effect_col in varchar2,
P_1_t_val in varchar2,
P_where in varchar2 default null,
P_refc in out refc) is
V_ SQL varchar2 (4000 );
Type v_keep_ind_by is table of varchar2 (4000) index by binary_integer;
V_keep v_keep_ind_by;
Type v_effect_ind_by is table of varchar2 (4000) index by binary_integer;
V_1_v_1_t_ind_by;
V_keep_cnt int;
V_group_by varchar2 (2000 );
Begin
V_keep_cnt: = length (p_keep_cols)-length (replace (p_keep_cols, ',') + 1;
For I in 1 .. v_keep_cnt Loop
V_keep (I): = f_split_str (p_keep_cols, ',', I );
End loop;
V_ SQL: = 'select' | 'Cast ('| p_effect_col |
'As varchar2 (200) as '| p_0000t_col | 'from' | p_table |
'Group by' | p_effect_col;
Execute immediate v_ SQL bulk collect
Into v_1;
For I in 1 .. v_keep.count Loop
V_group_by: = v_group_by | v_keep (I) | ',';
End loop;
V_group_by: = rtrim (v_group_by ,',');
V_ SQL: = 'select' | v_group_by | ',';

For X in 1 .. v_20.t.count Loop
V_ SQL: = v_ SQL | 'max (decode ('| p_effect_col |', '| CHR (39) |
V_1_( x) | CHR (39) | ',' | p_1_t_val |
', Null) as "' | v_1_( x) | '",';
End loop;
V_ SQL: = rtrim (v_ SQL ,',');
If p_where is not null then
V_ SQL: = v_ SQL | 'from' | p_table | p_where | 'group by' |
V_group_by;
Else
V_ SQL: = v_ SQL | 'from' | p_table | 'group by' | v_group_by;
End if;
P_print_ SQL (v_ SQL );
Open p_refc for v_ SQL;
Exception
When others then
Open p_refc
Select 'x' from dual where 0 = 1;
End;

End;

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.