1. 程式人生 > >Oracle合併資料的方法 wm_concat() 和 listagg()

Oracle合併資料的方法 wm_concat() 和 listagg()

一、簡介

在實際專案中進行一些統計資料時,難免會遇到需要合併資料在列表進行展示的需求。本文將講解一下oracle 11g合併資料的兩種方法 (wm_concat() 和 listagg())。示例:

--with as 相當於一張臨時表(一次分析,多次使用)  
with temp as
 (select 'male' as sex, 'zhangsan' as stu_name
    from dual
  union
  select 'male' as sex, 'lisi' as stu_name
    from dual
  union
  select 'female' as sex, 'xiaohong' as stu_name
    from dual
  union
  select 'female' as sex, 'xiaozhang' as stu_name
    from dual
  union
  select 'male' as sex, 'zhaoliu' as stu_name
    from dual)
    
select t.* from temp t;

在沒有合併資料之前,資料查詢出來可能有一些欄位重複,如下圖:

 

下面將性別相同的合併起來進行展示。

二、第一種方法: wm_concat() 

--with as 相當於一張臨時表(一次分析,多次使用)  
with temp as
 (select 'male' as sex, 'zhangsan' as stu_name
    from dual
  union
  select 'male' as sex, 'lisi' as stu_name
    from dual
  union
  select 'female' as sex, 'xiaohong' as stu_name
    from dual
  union
  select 'female' as sex, 'xiaozhang' as stu_name
    from dual
  union
  select 'male' as sex, 'zhaoliu' as stu_name
    from dual)
    
--第一種方法: 使用 wm_concat()方法進行合併,預設以逗號分隔   
select t.sex as sex, wm_concat(t.stu_name) as stu_name
  from temp t
 group by t.sex;

查詢結果:

 

如果想替換分隔的字元:

--with as 相當於一張臨時表(一次分析,多次使用)  
with temp as
 (select 'male' as sex, 'zhangsan' as stu_name
    from dual
  union
  select 'male' as sex, 'lisi' as stu_name
    from dual
  union
  select 'female' as sex, 'xiaohong' as stu_name
    from dual
  union
  select 'female' as sex, 'xiaozhang' as stu_name
    from dual
  union
  select 'male' as sex, 'zhaoliu' as stu_name
    from dual)
    
--如果不想使用逗號分隔,可以替換成其他字元
select t.sex as sex, replace(wm_concat(t.stu_name), ',', '|') as stu_name
  from temp t
 group by t.sex;

查詢結果:

三、第二種方法: listagg()

--with as 相當於一張臨時表(一次分析,多次使用)  
with temp as
 (select 'male' as sex, 'zhangsan' as stu_name
    from dual
  union
  select 'male' as sex, 'lisi' as stu_name
    from dual
  union
  select 'female' as sex, 'xiaohong' as stu_name
    from dual
  union
  select 'female' as sex, 'xiaozhang' as stu_name
    from dual
  union
  select 'male' as sex, 'zhaoliu' as stu_name
    from dual)
    
--第二種方法: 使用listagg()方法,可以指定分隔的字元
select t.sex as sex,
       listagg(t.stu_name,',') within group(order by t.stu_name) as stu_name
  from temp t
 group by t.sex;

注意: 需要加上within group(order by ...)

查詢結果:

如果想用別的分隔字元,直接指定即可:

--with as 相當於一張臨時表(一次分析,多次使用)  
with temp as
 (select 'male' as sex, 'zhangsan' as stu_name
    from dual
  union
  select 'male' as sex, 'lisi' as stu_name
    from dual
  union
  select 'female' as sex, 'xiaohong' as stu_name
    from dual
  union
  select 'female' as sex, 'xiaozhang' as stu_name
    from dual
  union
  select 'male' as sex, 'zhaoliu' as stu_name
    from dual)
    
--如果需要替換分隔字元,直接指定其他字元即可
select t.sex as sex,
       listagg(t.stu_name,'|') within group(order by t.stu_name) as stu_name
  from temp t
 group by t.sex;

查詢結果:

四、總結

以上是關於oracle 合併資料的兩種方法,本文是作者在實際專案中遇到這樣的需求進行的一些在總結和方法,僅供大家學習參考,共同學習共同進步。