聚合函數 listagg (超出長度限制時xmlagg)


 

表&數據

CREATE TABLE peoplebuy (people Varchar2(10),buy Varchar2(10),price NUMBER);
INSERT INTO peoplebuy VALUES ('','小貓',10);
INSERT INTO peoplebuy VALUES ('','小狗',200);
INSERT INTO peoplebuy VALUES ('','',20);

原來的結果

SELECT * FROM PEOPLEBUY ORDER BY PEOPLE;

想要的結果

SELECT PEOPLE,
       LISTAGG(BUY, ',') WITHIN GROUP(ORDER BY BUY DESC) AS BUY,
       SUM(PRICE) TOTAL
  FROM PEOPLEBUY
 GROUP BY PEOPLE
 ORDER BY PEOPLE

 

用法

LISTAGG(measure_expr [, 'delimiter'])
  WITHIN GROUP (order_by_clause) [OVER query_partition_clause]

 

當連接的字符串過長時會出現以下錯誤:

解決方案:

XMLAGG https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions215.htm

XMLType https://docs.oracle.com/cd/B10501_01/appdev.920/a96616/arxml24.htm

RTRIM(XMLAGG(XMLELEMENT(e, t.signature_id, ',').extract('//text()')).getClobVal(),',') very_long_text

聚合元素,提取文本,獲取clob,去空格

 

Function Description

XMLType()

Constructor that constructs an instance of the XMLType datatype. The constructor can take in the XML as a CLOB, VARCHAR2 or take in a object type.

createXML()

Static function for creating and returning an XMLType instance.

existsNode()

Takes a XMLType instance and a XPath and returns 1 or 0 indicating if applying the XPath returns a non-empty set of nodes.

extract()

Takes a XMLType instance and an XPath, applies the XPath expression and returns the results as an XMLType.

isFragment()

Checks if the input XMLType instance is a fragment or not. A fragment is a XML instance, which has more than one root element.

getClobVal()

Returns the value of the XMLtype instance as a CLOB

getNumVal()

Returns the value of the XMLtype instance as a NUMBER. This is only valid if the input XMLtpye instance contains a simple text node and is convertible to a number.

getStringVal()

Returns the value of the XMLType instance as a string.

transform()

Takes an XMLtype instance and an associated stylesheet (which is also an XMLtype instance) , applies the stylesheet and returns the result as XML.

toObject()

Converts the XMLType instance to an object type.

isSchemaBased()

Returns 1 or 0 indicating if the input XMLType instance is a schema based one or not.

getSchemaURL()

Returns the XML schema URL if the input is a XMLSchema based.

getRootElement()

Returns the root element of the input instance. Returns NULL if the instance is a fragment

createSchemaBasedXML()

Creates a schema based XMLtype instance from the non-schema based instance using the input schema URL.

createNonSchemaBasedXML()

Creates a non schema based XML from the input schema based instance.

getNamespace()

Returns the namespace for the top level element in a schema based document.

schemaValidate()

Validates the input instance according to the XMLSchema. Raises error if the input instance is non-schema based.

isSchemaValidated()

Checks if the instance has been validated against the schema.

setSchemaValidated()

Sets the schema valid flag to avoid costly schema validation.

isSchemaValid()

Checks if the input instance is schema valid according to the given schema URL.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM