ORACLE的wmsys.wm_concat和listagg函数

oracle将多个结果合并展示成一行,有wmsys.wm_concat和listagg函数,之前我习惯用wmsys.wm_concat,但常常遇到有oracle版本不支持的问题。于是转用listagg。

1、wmsys.wm_concat() 连接结果用逗号分隔

select t.type, t.assoCode, 
	   to_char(wmsys.wm_concat(t.date)) date
  from openday t
 where t.type in ('1', '2')
   and t.date >= 20191201
   and t.date <= 20191210
 group by t.type, t.assoCode;

使用wm_concat可能会有字符类型不匹配的问题,需要加个to_char。to_char(wmsys.wm_concat(t.date)) 或者 wmsys.wm_concat(to_char(t.date))

示例:

typeassoCodedate
1320191201,20191208,20191207
1420191208,20191207

2、listagg() within group () 将多条结果合并展示成1行,可以指定分隔符’,'

select t.type, t.assoCode, 
	   listagg(t.date,',') within group(order by t.date) date
  from openday t
 where t.type in ('1', '2')
   and t.date >= 20191201
   and t.date <= 20191210
 group by t.type, t.assoCode;

示例:与wmsys.wm_concat效果基本一致,就是有排序的差别

typeassoCodedate
1320191201,20191207,20191208
1420191207,20191208

3、listagg() within group () over (partition by …) 将多条结果连接,但各自显示一行

select t.type,t.assoCode,
       listagg(t.date,',') within group(order by t.date)over (partition by t.type,t.assoCode) date
 from openday t 
 where t.type in ('1', '2')
      and t.date>=20191201 
      and t.date<=20191210

使用了partition by就不要再group by了。都是分组,前者不去重。
示例:

typeassoCodedate
1320191201,20191207,20191208
1320191201,20191207,20191208
1320191201,20191207,20191208
1420191207,20191208
1420191207,20191208

这种partition by 的方法可以用于既要计数,又要展示的情况:

select * from
(select t.type,t.assoCode,
       listagg(t.trans_date,',') within group(order by t.date)over (partition by t.type,t.assoCode) date
 from openday t 
 where t.type in ('1', '2')
      and t.date>=20191201 
      and t.date<=20191210) v
group by v.type,v.assoCode,v,date

示例:

typeassoCodedatecount
1320191201,20191207,201912083
1420191207,201912082
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值