Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to order the fields getting aggregated by XMLAGG function?

Tags:

sql

xml

I have the below table:

Part_number   seq_nbr    Super_narrative
1                1        When replacing
1                2        part  with following parts
1                3        note:follwing are now available
1                4        with the organization 

I am using the XMLAGG to aggregate the super_narrative in 1 line corresponding to part_number.

SELECT serv_part_nbr, rtrim (xmlagg (xmlelement (e, Super_narrative || ', ')).extract ('//text()'), ',') PHRASE_TEXT 
FROM BL_MACS_SUPERSEDENCE_NOTE
GROUP BY part_number

But I want to aggregate the super_narrative according to seq_nbr.

How to do that??

like image 454
user3626931 Avatar asked May 12 '14 03:05

user3626931


People also ask

What is Xmlagg in SQL?

The XMLAGG function takes a list of XML elements from one column and returns an aggregated XML document in a single cell.

How do I use Xmlagg instead of Listagg?

We can't eliminate duplicates inside XMLAGG. For LISTAGG, we can use the DISTINCT keyword. The return data type of XMLAGG is always XML. So depending on how the result is used, the output of XMLAGG might need to be cast to the data type that the application requires.

What is Xmlagg in Teradata?

The XMLAGG function is used to perform an aggregate of multiple rows. This can used for multiple purpose such as constructing XML value or merging rows string values into a single row value.

What is the use of xmlagg function?

The XMLAGG function is used to perform an aggregate of multiple rows. This can used for multiple purpose such as constructing XML value or merging rows string values into a single row value. The XMLAGG function concatenates values of a table column from multiple rows into a single string.

What is the difference between xmlagg and order by?

The XMLAGG function concatenates values of a table column from multiple rows into a single string. The ORDER BY clause is applied to the query result set after all aggregate fields are evaluated, ORDER BY cannot directly affect the sequence of values within this string.

How to remove trailing comma in the aggregated text using xmlagg?

The xmlagg creates an XML snippet that is an aggregation of the x XML elements. “.extract (‘//text ()’)” get rids of xml keeping the string alone. Finally we will remove trailing comma in the aggregated text using rtrim.

How to aggregate the collection of fragments of XML in Oracle?

We can use the XMLAGG function in the oracle database to aggregate the collection of fragments of XML. This function returns the value which will be an aggregated document in XML and if any of the arguments that are passed to this function evaluate to NULL then the argument is skipped or dropped out from the final result.


1 Answers

You don't mention DBMS, but a wild guess would be:

SELECT serv_part_nbr
              , rtrim (xmlagg (xmlelement(e, Super_narrative || ', ') order by seq_nbr ).extract ('//text()'), ','
                          ) PHRASE_TEXT 
 FROM BL_MACS_SUPERSEDENCE_NOTE
 GROUP BY part_number
like image 137
Lennart Avatar answered Oct 24 '22 10:10

Lennart