oracle-listagg()函数实现列转行

本文介绍了如何利用Oracle的listagg()函数将多列数据拼接成一行,以解决查询结果中重复列的问题。通过示例代码展示了listagg()的使用方法,以及与不使用该函数时的区别。建议在需要拼接字段时优先考虑使用listagg(),因为它既简单又准确。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

      查询product_name字段时,出现多个数据,为避免多列展示,需要使用||进行拼接,这时可以使用oracle的listagg()函数进行列转行,代码如下
 select cont.ContNo AS policy_no,
       substr(cont.SupplierCode, 0, 4) AS supplier_code,
       (select listagg((select risk.riskname
                         from fmrisk risk
                        where risk.riskcode = f.riskcode),
                       '||') within GROUP(order by f.riskcode)
          from fcpol f
         where f.contno = cont.contno) AS product_name,
       cont.CValiDate AS effective_date,
       (case when cont.state in('01','04') then '1' when cont.state in('02','03') then '2' end) AS main_status,
       cont.InsuredName AS insured_name,
       cont.InnerContNo AS innerpolicy_no
  from fccont cont 
        使用listagg(字段,'||') within group(order by 字段),这是其固定格式,得到的结果为:xxx||xxx,成功实现
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值