查询子表某个字段的值,如果存在多个重复,用‘/’分隔

例:lcapp为主表,lcappg为子表,查询子表lcappg的prjcode,如果存在多个重复,用‘/’分隔

lcapp和lcappg 通过 lcappicode 关联;

1、查询子表lcappg 中,lcappicode相同,但是prjcode多个的数据。

思路:先用distinct去重复,然后通过lcappicode分组,这样就能找出lcappicode相同,但是prjcode多个非重复的数据。

distinct作用于多列,实际上是根据多个字段来去重的。

例如:select  user_id,note from morders  order by user_id;

select distinct user_id,note from morders  order by user_id;

通过user_id和note 来去重

select lcappicode, count(*)
  from (select distinct lcappicode, lg.prjcode from lcappg lg) t
 group by t.lcappicode
having count(*) > 1

2、oracle  wm_concat函数,拼接prjcode。

     wm_concat()得到的数据为clob类型,可用to_char()转成varchar2类型;

     wm_concat()默认使用逗号分隔,可用replace()替换成‘/’

select to_char(replace(wm_concat(distinct t.prjcode), ',', '/'))
  from lcappg t
 where t.lcappicode = 'N9XYG0000000000002211';

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值