How can Oracle columns be merged into one row?

In Oracle, you can use the LISTAGG function to combine multiple row values of a column into one single row.

Syntax is as follows:
LISTAGG(column_name, separator) WITHIN GROUP (ORDER BY column_name)

column_name represents the name of the column to be merged, while separator represents the delimiter between the merged row values.

For example, if we have a table named employees with a column named last_name, we can concatenate all the last_name values into one row, separating them by commas, by using the following statement:
SELECT LISTAGG(last_name, ‘,’) WITHIN GROUP (ORDER BY last_name) AS merged_last_names
FROM employees;

This will return a row containing all the last_name values concatenated, with a comma as the separator.

bannerAds