Oracle table column/column conversion)

Source: Internet
Author: User
Http://www.cublog.cn/opera/showart.php? Blogid = 14788 & id = 141763
Quote:

[Q] how to convert rows and columns
[A]
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
......
The statement is as follows:
Select student, sum (decode (subject, 'China', grade, null) "",
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 must 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;
/SQL> select distinct C1, get_c2 (C1) CC2 from table;

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.