Oracle row and column Conversion

Source: Internet
Author: User

1. Column and column conversion with fixed columns
For example
Student subject grade
---------------------------
Student1 language 80
Student1 mathematics 70
Student1 English 60
Student2 language 90
Student2 mathematics 80
Student2 100 English
......
Convert
Chinese, mathematics, and English
Student1 80 70 60
Student2 90 80 100
......
Statement: select student,
Sum (decode (subject, 'China', grade, null) "language ",
Sum (decode (subject, 'mat', grade, null) "Mathematics ",
Sum (decode (subject, 'English ', grade, null) "English"
From table
Group by student;

2. Changing columns and columns
For example
C1 C2
--------------
1. Me
1 is
1 WHO
2 Zhi
2 channels
3 No
......
Convert
1. Who am I?
2. Know
3 No

This type of conversion can be completed by PL/SQL. Here is an example.
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;

Select distinct C1, get_c2 (C1) CC2 from table;

You can also use Analytic Functions and connect_by without PL/SQL:

Select C1, substr (max (sys_connect_by_path (C2, ';'), 2) Name
From (select C1, C2, RN, lead (RN) over (partition by C1 order by RN) rn1
From (select C1, C2, row_number () over (order by C2) Rn
From t ))
Start with rn1 is null
Connect by rn1 = prior Rn
Group by C1;

3. The number of columns is not fixed (columns in the crosstab chart are transposed)
This is a very troublesome one. You need to use PL/SQL:

raw data:
class1 calldate callcount
1 40
1 6
2 77
3 33
3 9
3 2005-08-07 21

After transpose:
Calldate callcount1 callcount2 callcount3
------------------------------------------
2005-08-09 0 0 33

6 0 21

The test is as follows:
1). Create test tables and data
Create Table T (
Class1 varchar2 (2 byte ),
Calldate date,
Callcount integer
);

Insert into T (class1, calldate, callcount)
Values ('1', to_date ('2014/1/123', 'Mm/DD/yyyy'), 40 );

Insert into T (class1, calldate, callcount)
Values ('1', to_date ('2014/1/123', 'Mm/DD/yyyy'), 6 );

Insert into T (class1, calldate, callcount)
Values ('2', to_date ('2014/1/123', 'Mm/DD/yyyy'), 77 );

Insert into T (class1, calldate, callcount)
Values ('3', to_date ('2014/1/123', 'Mm/DD/yyyy'), 33 );

Insert into T (class1, calldate, callcount)
Values ('3', to_date ('2014/1/123', 'Mm/DD/yyyy'), 9 );

Insert into T (class1, calldate, callcount)
Values ('3', to_date ('2014/1/123', 'Mm/DD/yyyy'), 21 );

Commit;

2) create a ref cursor to prepare the output result set.
Create or replace package pkg_getrecord
Is
Type myrctype is ref cursor;
End pkg_getrecord;
/

3) create a dynamic SQL cross tabulation function to output the result set.
Create or replace function fn_rs
Return pkg_getrecord.myrctype
Is
S varchar2 (4000 );
Cursor C1 is
Select ', sum (case when class1 ='
| Class1
| 'Then callcount else 0 end )'
| '"Callcount'
| Class1
| '"'C2
From t
Group by class1;
R1 C1 % rowtype;
List_cursor pkg_getrecord.myrctype;
Begin
S: = 'select calldate ';
Open C1;
Loop
Fetch C1 into R1;
Exit when C1 % notfound;
S: = S | r1.c2;
End loop;
Close C1;
S: = S | 'from T group by calldate order by calldate DESC ';
Open list_cursor for S;
Return list_cursor;
End fn_rs;
/

4 ). run the test in SQL plus:
var results refcursor;
Exec: Results: = fn_rs;
print results;
calldate callcount1 callcount2 callcount3
--------------- ----------
0 0 33
40 77 9
6 0 21

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.