Listparts, 11 glistparts

Source: Internet
Author: User

Listparts, 11 glistparts

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 column-to-row and row-to-Column

In oracle, you can use the decode function to process:

Select Product Name,
Sum (decode (quarter, 'quarter 11', sales, 0) first quarter sales,
Sum (decode (quarter, 'quarter 2 ', sales, 0) second quarter sales,
Sum (decode (quarter, 'quarter 3 ', sales, 0) third quarter sales,
Sum (decode (quarter, 'quarter 11', sales, 0) Fourth quarter sales,
From table name
Group by product name;

ORACLE same table column forwarding

Create table test (a1 number, a2 number, a3 number); insert into test values (, 3); insert into test values (, 13 ); dexter @ REPO> select a from (2 SELECT * 3 FROM test 4 undo (5 a 6 for v 7 IN (a1, a2, a3) 8 )); A ---------- 1 2 3 11 12 13 6 rows have been selected. It uses the 11g column transpose function, unpovit. The Data Types of these columns must be consistent. Give it a try.


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.