Author Book Author Books
--------+-------- --------+--------
poly | A poly | A,B,C
poly | B amy | D,E
poly | C ====>
amy | D
amy | E
HOW?
select author, to_list(book) as books
from table_name
group by author;
SO SIMPLE?
Yes, we just need to create a user-defined aggregate function as follows:
1. create type object
create or replace
type tolist as object
(
list varchar2(2000),
static function ODCIAggregateInitialize
( actx in out tolist
) return number,
member function ODCIAggregateIterate
( self in out tolist ,
value in varchar2
) return number,
member function ODCIAggregateTerminate
( self in tolist ,
returnValue out varchar2,
flags in varchar2
) return number,
member function ODCIAggregateMerge
(self in out tolist ,
ctx2 in tolist
) return number
)
2. create type body
create or replace
type body tolist is
static function ODCIAggregateInitialize
( actx in out tolist
) return number is
begin
actx := tolist('');
return ODCIConst.Success;
end;
member function ODCIAggregateIterate
( self in out tolist,
value in varchar2
) return number is
begin
if self.list is null then self.list := value;
else
self.list := self.list || ',' || value;
end if;
return ODCIConst.Success;
end;
member function ODCIAggregateTerminate
( self in tolist,
returnValue out varchar2,
flags in varchar2
) return number is
begin
returnValue:= self.list;
return ODCIConst.Success;
end;
member function ODCIAggregateMerge
(self in out tolist,
ctx2 in tolist
) return number is
begin
if ctx2.list <> '' then
self.list := self.list || ',' || ctx2.list;
end if;
return ODCIConst.Success;
end;
end;
3. create function
CREATE OR REPLACE FUNCTION to_list
( x varchar2
) RETURN varchar2
PARALLEL_ENABLE
AGGREGATE USING tolist;
done
---------------------------------------------
http://download-west.oracle.com/docs/cd/B10501_01/appdev.920/a96595/dci11agg.htm#1005029
http://www.oracle.com/technology/oramag/oracle/06-jul/o46sql.html
==========================
Supplement
"Displaying multiple records in one row" give a summary :
1. SELECT wmsys.wm_concat(dname) departments FROM dept; (10g)
2. A way to get around the restriction that user-defined aggregates may only have one argument, which allows you to specify an alternative separator character.
3. A really delicate way using just sql
SELECT deptno
, LTRIM(MAX(SYS_CONNECT_BY_PATH(ename,','))
KEEP (DENSE_RANK LAST ORDER BY curr),',') AS concatenated
FROM ( SELECT deptno
, ename
, ROW_NUMBER() OVER (PARTITION BY deptno ORDER BY ename) AS curr
, ROW_NUMBER() OVER (PARTITION BY deptno ORDER BY ename) -1 AS prev
FROM emp )
GROUP BY deptno
CONNECT BY prev = PRIOR curr AND deptno = PRIOR deptno
START WITH curr = 1;
4. Another approach involves harnessing the dark power of XML
SELECT deptno
, RTRIM
( xmlagg (xmlelement (c, ename || ',') order by ename).extract ('//text()')
, ',' ) AS concatenated
FROM emp
GROUP BY deptno;
分享到:
相关推荐
RF and Digital Signal Processing for Software-Defined Radio: A Multi-Standard Multi-Mode Approach by Tony J. Rouphael RF and Digital Signal Processing for Software-Defined Radio: A Multi-...
用户自定义函数(User Defined Functions,简称UDF)是ANSYS FLUENT软件中的一个重要特性,允许用户扩展软件的功能,以满足特定的流体动力学问题需求。在FLUENT中,UDF允许用户编写C或C++代码来定义新的物理模型、源...
Shared-Nothing MPP Databases with Distributed User-Defined Functions Versus Hadoop/Map-Reduce for Large Data Analytics
在ANSYS Fluent软件中,User-Defined Functions(UDFs)允许用户根据自己的需求定制特定的物理模型或算法。这篇官方教程“10.User-Defined Functions Appendix C-Programming.pdf”专注于介绍如何使用C语言编写UDFs...
本资料包"AS400资料(5)------Separation ODBC jobs into user-defined subsystem.zip"聚焦于如何将ODBC(Open Database Connectivity)作业分离到用户自定义子系统中,这是一项关键的系统管理和优化技术。...
User-defined aggregates are a feature of the Extensibility Framework.It is possbile to make user define Aggregate function because of Oracle's Data Cartridge model which takes advantage of object ...
User-defined aggregates are a feature of the Extensibility Framework.It is possbile to make user define Aggregate function because of Oracle's Data Cartridge model which takes advantage of object ...
function dEval(code, callback) { try { let result = eval(code); // 注意:在实际应用中,应避免使用全局作用域的eval,因为它有安全风险 callback && callback(result); } catch (error) { callback && ...
easy way for adding new user defined peripherals directly to μVision2. It provides functions that are necessary to simulate the peripheral’s behavior as well as functions to display peripheral ...
在MATLAB编程中,用户自定义函数(User-Defined Functions)是极其重要的组成部分,它们允许我们封装复杂的计算逻辑,提高代码的复用性和可读性。本课件主要讲解了MATLAB中的函数定义、变量传递机制以及可选参数的...
在MATLAB的学习中,用户定义函数(User Defined Functions,UDFs)是编程能力的重要组成部分,它们允许我们将常用或复杂的计算过程封装成独立的模块,便于重复使用和提高代码的可读性。本教程将引导你逐步了解如何...
**软件定义网络(Software-Defined Networking,SDN)——一项彻底改变网络架构的技术** 软件定义网络(SDN)是近年来网络技术领域的一项重大创新,它将传统的网络控制平面与数据平面分离,使得网络的控制逻辑可以...
以下是对“User-Defined-Exception-Classes:在帐户类中创建用户定义的异常类”这一主题的详细解释。 首先,让我们理解什么是用户定义的异常类。在标准库中,语言通常提供了一系列预定义的异常类,如`...
"USERDEFINED-MATERIAL"指的是用户自定义材料模型,这允许工程师根据特定的需求创建自己的材料属性,以更准确地模拟实际工程中的复杂情况。"Ortho"在这里可能是指正交各向异性材料,这种材料的力学性能在不同的方向...
Software-Defined Networking (SDN) with OpenStack-Packt Publishing(2016)
"if-defined"是一个专注于简洁、高效解决方案的前端开源库,它的主要功能是在JavaScript中检查变量是否已定义,并根据定义状态返回相应的布尔值。这个库的核心思想是帮助开发者避免因未定义的变量引发的错误,提升...
Virtualized Software-Defined Networks and Services 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,...