侧边栏壁纸
博主头像
丛庆

没事儿写代码,有事写代码。email:1024@cong.zone

  • 累计撰写 116 篇文章
  • 累计创建 97 个标签
  • 累计收到 4 条评论

【Oracle】多字段值拼接后去重

丛庆
2022-05-20 / 0 评论 / 0 点赞 / 711 阅读 / 911 字 / 正在检测是否收录...
温馨提示:
部分资料和图片来源于网络,如有危害到您的利益请与我联系删除,1024@cong.zone。

表结构

ID PRO_CODE BIZ_NAME DEPARTMENT ENABLE_WATCH
uuid1 0001 BIZ1 2 0
uuid 2 0001 BIZ1 2 0

要求

表中 PRO_CODE拼接BIZ_NAME的结果在表中唯一

SQL

拼接组合字段生成子表

select ID,
       concat(PRO_CODE, BIZ_NAME) as pro_biz
from t_product
where DEPARTMENT = '2'
  and ENABLE_WATCH = '0'

将重复的行ID拼接成一行用 , 分隔

select listagg(id, ',') within group (ORDER BY pro_biz ) as id
from (
         select ID,
                concat(PRO_CODE, BIZ_NAME) as pro_biz
         from t_product
         where DEPARTMENT = '2'
           and ENABLE_WATCH = '0') p
group by p.pro_biz
having count(p.pro_biz) > 1 )

将一行id转成多行作为in的条件

select regexp_substr(idd, '[^,]+', 1, ROWNUM) iddd
from (select regexp_replace(id, '^(.*?),', '') as idd
      from (select listagg(id, ',') within group (ORDER BY pro_biz ) as id
            from (
                     select ID, concat(PRO_CODE, BIZ_NAME) as pro_biz
                     from t_product
                     where DEPARTMENT = '2'
                       and ENABLE_WATCH = '0') p
            group by p.pro_biz
            having count(p.pro_biz) > 1) p2) p3
connect by ROWNUM <= length(idd) - length(regexp_replace(idd, ',', '')) + 1;
0

评论区