The fuction of listagg.

LISTAGG orders data within each group specified in the ORDER BY clause and then concatenates the values of the <measure> column.

  • As a single-set aggregate function, LISTAGG operates on all rows and returns a single output row.

  • As a group-set aggregate, the function operates on and returns an output row for each group defined by the GROUP BY clause.

  • As an analytic function, LISTAGG partitions the query result set into groups based on one or more expression in the query_partition_clause .

SQL> select * from v$version;

BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
PL/SQL Release 11.2.0.1.0 - Production
CORE    11.2.0.1.0      Production
TNS for 32-bit Windows: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production

SQL> ed
已写入 file afiedt.buf

  1  select deptno, listagg(job,';') within group (order by job) jobs
  2    from  scott.emp
  3    group by deptno
  4*  order by deptno
SQL> /

    DEPTNO
----------
JOBS
--------------------------------------------------------------------------------
        10
CLERK;MANAGER;PRESIDENT

        20
ANALYST;ANALYST;CLERK;CLERK;MANAGER

        30
CLERK;MANAGER;SALESMAN;SALESMAN;SALESMAN;SALESMAN

We can use the distinct in the sub query to remove the duplicated records.
SQL> ed
已写入 file afiedt.buf

  1  select deptno, listagg(job,';') within group (order by job) jobs
  2     from (select distinct deptno, job from scott.emp)
  3     group by deptno
  4*   order by deptno
SQL> /

    DEPTNO
----------
JOBS
--------------------------------------------------------------------------------
        10
CLERK;MANAGER;PRESIDENT

        20
ANALYST;CLERK;MANAGER

        30
CLERK;MANAGER;SALESMAN

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值