Oracle row and column conversion ____oracle

Source: Internet
Author: User
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 can be done with the help of Pl/sql, which gives 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;




or without pl/sql, using analytic functions and CONNECT_BY implementations:


SELECT C1, SUBSTR (MAX (Sys_connect_by_path (C2, '; ')), 2) NAME
From (SELECT C1, c2, RN, leads (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 (cross table row transpose)
This is the more troublesome one, need to rely on Pl/sql:


Raw data:
CLASS1 calldate Callcount
1 2005-08-08 40
1 2005-08-07 6
2 2005-08-08 77
3 2005-08-09 33
3 2005-08-08 9
3 2005-08-07 21


After transpose:
Calldate CallCount1 CallCount2 CallCount3
------------ ---------- ---------- ----------
2005-08-09 0 0 33
2005-08-08 40 77 9
2005-08-07 6 0 21


The experiment is as follows:
1. Establish 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 (' 08/08/2005 ', ' mm/dd/yyyy '), 40);


INSERT into T (Class1, calldate, Callcount)
VALUES (' 1 ', to_date (' 08/07/2005 ', ' mm/dd/yyyy '), 6);


INSERT into T (Class1, calldate, Callcount)
VALUES (' 2 ', to_date (' 08/08/2005 ', ' mm/dd/yyyy '), 77);


INSERT into T (Class1, calldate, Callcount)
VALUES (' 3 ', to_date (' 08/09/2005 ', ' mm/dd/yyyy '), 33);


INSERT into T (Class1, calldate, Callcount)
VALUES (' 3 ', to_date (' 08/08/2005 ', ' mm/dd/yyyy '), 9);


INSERT into T (Class1, calldate, Callcount)
VALUES (' 3 ', to_date (' 08/07/2005 ', ' mm/dd/yyyy '), 21);


COMMIT;


2). Establish REF CURSOR prepare output result set
CREATE OR REPLACE PACKAGE Pkg_getrecord
Is
TYPE Myrctype is REF CURSOR;
End Pkg_getrecord;
/


3. Establish dynamic SQL crosstab function, output 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). Test executed under SQL Plus:
var results refcursor;
Exec:results: = fn_rs;
Print results;
Calldate CallCount1 CallCount2 CallCount3
--------------- ---------- ---------- ----------
2005-08-09 0 0 33
2005-08-08 40 77 9
2005-08-07 6 0 21




Practical Code:
SELECT roadseg_id, Roadseg_name, Maxspeed, Pointstr
From T_roadseg R, (SELECT C1, SUBSTR (MAX (Sys_connect_by_path (C2, '; ')), 2 pointstr
From (SELECT C1,
C2,
Rn
Lead (RN) over (PARTITION by C1 Order by RN) RN1
From (SELECT roadseg_id C1,
LON | | ',' || LAT C2,
Row_number () over (the order by RP.) ROADSEG_ID) RN
From T_roadseg_points RP))
START with RN1 is NULL
CONNECT by RN1 = PRIOR RN
GROUP by C1) V
WHERE roadseg_id = 1
and roadseg_id = C1
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.