Implementation of oracle Column Merging

  • 2021-12-11 19:22:58
  • OfStack

We will use oracle's column merging in many occasions, and oracle provides the following methods to realize column merging:

1. Oracle 10G formerly used WMSYS.WM_CONCAT:

wmsys. wm_concat separates the values of the fields by ",".


select id,wm_concat(name) from tab_name group by id;

2. Use sys_connect_by_path

sys_connect_by_path (field name, connection symbol between two fields). Do not use commas for connection symbols here. oracle will report errors. If 1 must be used, replace can be used instead of 1. The method is as follows: REPLACE (field name, original character, ','). A tree must be built before this function is used, otherwise it is useless.

3. Oracle 11G You can use listagg later


select listagg(id,',') within group (order by id) col_name from tab_name;

4. Use custom functions:


create or replace function getRow(table1 varchar2 , ptdb1 varchar2) RETURN VARCHAR2 is
Result VARCHAR2(1000);
begin
FOR cur IN (SELECT audit_code FROM sys_audit_column t2 WHERE table1 =
t2.table_name and ptdb1 = t2.ptdb_name) LOOP
RESULT := RESULT||cur.audit_code||',';
END LOOP;
RESULT:=rtrim(RESULT,',');
return(Result);
end getRow;

Related articles: