[Repost] Oracle's listagg () WITHIN GROUP () function uses Oracle's listagg () WITHIN GROUP () function

Oracle's listagg () WITHIN GROUP () function

 
https: // www.cnblogs.com/sjxbg/p/9859100.html I 

wrote a similar statement in 2018 but I have no impression at all: 

listagg (cloumn, ' conconate marker ' ) within group (order by column)

 

1. Use conditional query Query the list of employees with department 20

    -Query the list of employees with department 20
    SELECT t.DEPTNO, t.ENAME FROM SCOTT.EMP t where t.DEPTNO = '20';

    Effect:


2. Use listagg () WITHIN GROUP () to merge multiple lines into one line

    SELECT
        T .DEPTNO,
        listagg (T .ENAME, ',') WITHIN GROUP (ORDER BY T .ENAME) names
    FROM
        SCOTT.EMP T
    WHERE
        T .DEPTNO = '20'
    GROUP BY
        T .DEPTNO

    effect:


3. Use listagg () within GROUP () over to record multiple rows on one line.

    SELECT
        T .DEPTNO,
        listagg (T .ENAME, ',') WITHIN GROUP (ORDER BY T .ENAME) over (PARTITION BY T .DEPTNO)
    FROM
        SCOTT.EMP T
    WHERE
        T .DEPTNO = '20' 

    effect:


        Note: The table data used is the emp (employee) table under the oracle user scott

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

When you have two columns A and B in table X, the data is as follows

A  B

a  1

a  2

a  3

b  1

b  2

b  3

To display data in a | 1 | 2 | 3, b | 1 | 2 | 3 format, use listagg ()

1. Use listagg () + group by

select A,B,listagg(B,'|') within GROUP (order by A)  C from X group by A;

 over (partition by class order by sroce) Accumulate according to sroce sorting, order by is a default windowing function, and partition by class.

2、使用listagg() + over(partition by ?)

select A,B listagg(B,'|') within Group(order by A) over(partition by A)  C from X;


DBMS_LOB.SUBSTR (col1) does not pass other parameters is to read all

DBMS_LOB.SUBSTR (col1,10,1) means take 10 bytes from the first byte

DBMS_LOB.SUBSTR (CLOB_VAR, 32767) means to intercept all data saved by CLOB variables

DBMS_LOB.FILECLOSE (IMG_BFILE) close the file

People are timid and run with big swords in their hands.

1. Use conditional query Query the list of employees with department 20

    -Query the list of employees with department 20
    SELECT t.DEPTNO, t.ENAME FROM SCOTT.EMP t where t.DEPTNO = '20';

    Effect:


2. Use listagg () WITHIN GROUP () to merge multiple lines into one line

    SELECT
        T .DEPTNO,
        listagg (T .ENAME, ',') WITHIN GROUP (ORDER BY T .ENAME) names
    FROM
        SCOTT.EMP T
    WHERE
        T .DEPTNO = '20'
    GROUP BY
        T .DEPTNO

    effect:


3. Use listagg () within GROUP () over to record multiple rows on one line.

    SELECT
        T .DEPTNO,
        listagg (T .ENAME, ',') WITHIN GROUP (ORDER BY T .ENAME) over (PARTITION BY T .DEPTNO)
    FROM
        SCOTT.EMP T
    WHERE
        T .DEPTNO = '20' 

    effect:


        Note: The table data used is the emp (employee) table under the oracle user scott

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

When you have two columns A and B in table X, the data is as follows

A  B

a  1

a  2

a  3

b  1

b  2

b  3

To display data in a | 1 | 2 | 3, b | 1 | 2 | 3 format, use listagg ()

1. Use listagg () + group by

select A,B,listagg(B,'|') within GROUP (order by A)  C from X group by A;

 over (partition by class order by sroce) Accumulate according to sroce sorting, order by is a default windowing function, and partition by class.

2、使用listagg() + over(partition by ?)

select A,B listagg(B,'|') within Group(order by A) over(partition by A)  C from X;


DBMS_LOB.SUBSTR (col1) does not pass other parameters is to read all

DBMS_LOB.SUBSTR (col1,10,1) means take 10 bytes from the first byte

DBMS_LOB.SUBSTR (CLOB_VAR, 32767) means to intercept all data saved by CLOB variables

DBMS_LOB.FILECLOSE (IMG_BFILE) close the file

Guess you like

Origin www.cnblogs.com/jinanxiaolaohu/p/12717308.html