需要帮助从查询中删除最后一个逗号

时间:2018-05-13 07:41:41

标签: sql regex database oracle

我有一个以下查询,它返回我的开放操作所有者的名称,并在regex的帮助下从逗号分隔值中删除重复记录。

OpenActionUsers
  AS ( SELECT DISTINCT action.INVESTIGATION_ID
            , REGEXP_REPLACE(
                LTRIM(
                  MAX(
                    SYS_CONNECT_BY_PATH(person.Last_Name || ' ' || person.First_Name || ', ', '<br/>')
                  )), 
              '([^,]+)(,[ ]*\1)+|(?<=(.*?, )\1)(.+?)<[^>]+>|\&(nbsp;)|(amp;)([^ ]+)( [ ]*\2)+', '\1') AS OPEN_ACTION_USERS
            , LISTAGG(userInfo.USER_ID, ', ') WITHIN GROUP (ORDER BY userInfo.USER_ID) AS OPEN_ACTION_USER_IDS
         FROM OpenActionInfo action
         JOIN ETK_User userInfo ON action.ACTION_OWNER = userInfo.USER_ID
         JOIN ETK_Person person ON userInfo.Person_Id = person.Person_Id
         GROUP BY action.INVESTIGATION_ID
         CONNECT BY action.INVESTIGATION_ID = PRIOR action.INVESTIGATION_ID AND RN = PRIOR RN + 1 START WITH RN = 1
     )

例如,这就是我的表列&#34; action_owner_name&#34;目前看起来像:

action_owner_name
------------------
Sam Williams,
Tom White,
Dan Andrews,
Chris Thomas, Sam Williams,
John Johnson, Dan Adnrews, Sean Gates,

如果有多条记录,我该如何从单条记录中删除逗号,以及从最后一条记录中删除逗号?

我希望渴望输出看起来像这样:

action_owner_name
------------------
Sam Williams
Tom White
Dan Andrews
Chris Thomas, Sam Williams
John Johnson, Dan Adnrews, Sean Gates

1 个答案:

答案 0 :(得分:2)

由于每个术语最后都有一个额外的逗号,你可以将其子串起来:

SELECT
    SUBSTR(OPEN_ACTION_USERS, 1, LENGTH(OPEN_ACTION_USERS) - 1) AS OPEN_ACTION_USERS,
    OPEN_ACTION_USER_IDS
FROM OpenActionUsers