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