Available in versions: Dev (3.20) | Latest (3.19) | 3.18 | 3.17 | 3.16 | 3.15 | 3.14

XMLAGG

Applies to ✅ Open Source Edition   ✅ Express Edition   ✅ Professional Edition   ✅ Enterprise Edition

A data set can be aggregated into a org.jooq.XML element using XMLAGG

SELECT xmlelement(
  NAME ids,
  xmlagg(xmlelement(NAME id, id))
)
FROM author
create.select(xmlelement("ids",
        xmlagg(xmlelement("id", AUTHOR.ID))
      ))
      .from(AUTHOR)
      .fetch();

The result would look like this:

+---------------------------------+
| xmlelement                      |
+---------------------------------+
| <ids><id>1</id><id>2</id></ids> |
+---------------------------------+

Ordering aggregation contents

When aggregating data into XML, ordering may be relevant. For this, use the ORDER BY clause in XMLAGG

SELECT xmlelement(
  NAME ids,
  xmlagg(xmlelement(NAME id, id) ORDER BY id DESC)
)
FROM author
create.select(xmlelement("ids",
        xmlagg(xmlelement("id", AUTHOR.ID))
          .orderBy(AUTHOR.ID.desc())))
      .from(AUTHOR)
      .fetch();

The result would look like this:

+---------------------------------+
| xmlelement                      |
+---------------------------------+
| <ids><id>2</id><id>1</id></ids> |
+---------------------------------+

Dialect support

This example using jOOQ:

xmlagg(xmlelement("id", AUTHOR.ID))

Translates to the following dialect specific expressions:

DB2, Oracle, Postgres, Teradata

xmlagg(xmlelement(NAME id, AUTHOR.ID))

ASE, Access, Aurora MySQL, Aurora Postgres, BigQuery, ClickHouse, CockroachDB, Derby, DuckDB, Exasol, Firebird, H2, HSQLDB, Hana, Informix, MariaDB, MemSQL, MySQL, Redshift, SQLDataWarehouse, SQLServer, SQLite, Snowflake, Sybase, Trino, Vertica, YugabyteDB

/* UNSUPPORTED */
Generated with jOOQ 3.20. Translate your own SQL on our website

References to this page

Feedback

Do you have any feedback about this page? We'd love to hear it!

The jOOQ Logo