Oracle 11g column-to-row listparts

Source: Internet
Author: User

Oracle 11g column-to-row listparts

It is a little difficult to switch between the first 11g of Oracle, and it is very simple after 11g. Currently, a test table records the approval information of a document. The id is the primary key of the Approval Information, the sheet_id is the foreign key, and the id of the document, remark indicates the approval content. On the previous list page, all the approval information of this document must be displayed in a grid.

SQL> drop table test purge;
SQL> create table test
(
Id number (10 ),
Sheet_id number (10 ),
Remark varchar2 (50)
);
SQL> insert into test values (1,100, 'approval comment 1 ');
SQL> insert into test values (2,100, 'approval comment 2 ');
SQL> insert into test values (3,100, 'approval comment 3 ');
SQL> insert into test values (4,200, 'agree 1 ');
SQL> insert into test values (5,200, 'agree 2 ');
SQL> insert into test values (6,200, 'agree 3 ');
SQL> insert into test values (7,300, 'roll back 1 ');
SQL> insert into test values (8,300, 'roll back 2 ');
SQL> commit;


SQL> col C_REMARK format a40;
SQL> select sheet_id, listparts (remark, ',') within GROUP (order by id) as c_remark
From test
Group by sheet_id;
SHEET_ID C_REMARK
--------------------------------------------------
100 approval comments 1, approval comments 2, approval comments 3
200 agree 1, agree 2, agree 3
300 roll back 1, roll back 2

There is also a way to write:

SQL> select sheet_id, listparts (remark, ',')
GROUP (
Order by id) over (partition by sheet_id) c_remark
From test;
SHEET_ID C_REMARK
--------------------------------------------------
100 approval comments 1, approval comments 2, approval comments 3
100 approval comments 1, approval comments 2, approval comments 3
100 approval comments 1, approval comments 2, approval comments 3
200 agree 1, agree 2, agree 3
200 agree 1, agree 2, agree 3
200 agree 1, agree 2, agree 3
300 roll back 1, roll back 2
300 roll back 1, roll back 2

SQL> select distinct sheet_id, listparts (remark, ',')
GROUP (
Order by id) over (partition by sheet_id) c_remark
From test;
SHEET_ID C_REMARK
--------------------------------------------------
200 agree 1, agree 2, agree 3
100 approval comments 1, approval comments 2, approval comments 3
300 roll back 1, roll back 2

Oracle 11g new collection function listparts implements column forwarding

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.