LISTAGG(column name, 'split symbol')
Only in versions above oracle 11g is a multi-line query result of the specified column name, which is combined into one line with the specified split symbol and displayed:
E.g:
Table raw data:
Requirement: Group the data in the mb1_Transport_License_list table according to the transportation_license_id data, and de-duplicate and merge the data in the Item_Category_Name column
Use the aggregate function LISTAGG to solve
- SELECT transportation_license_id,
- LISTAGG( to_char(Item_Category_Name), ',') WITHIN GROUP(ORDER BY Item_Category_Name) AS employees
- FROM ( select distinct transportation_license_id, item_category_name from mb1_Transport_License_list ) group by transportation_license_id
SQL parsing:
select distinct transportation_license_id, item_category_name from mb1_Transport_Lincense_list ; -- Deduplicate the data source data that needs to be merged. If the actual requirement does not require deduplication, you can directly change the table name (for example: from mb1_Transport_Lincense_list) to query
LISTAGG( to_char(Item_Category_Name), ',') WITHIN GROUP(ORDER BY Item_Category_Name) -- Split, merge and sort the content of the Item_Category_Name column with ", ";
to_char(Item_Category_Name) -- to_char(column name) to solve the problem of garbled query results after querying using the aggregate function LISTAGG;
Result after running: