oracle aggregate function LISTAGG, which combines the results of multiple rows into one row

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

 

 

[sql]  view plain copy  
 
  1. SELECT transportation_license_id,  
  2.      LISTAGG( to_char(Item_Category_Name), ',') WITHIN GROUP(ORDER BY Item_Category_Name) AS employees  
  3.       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:

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325158627&siteId=291194637