XMLAGG和RTRIM SQL查询中的重复值

时间:2016-12-07 06:55:44

标签: sql oracle oracle11gr2

以下查询带来重复值

SELECT maxuser.userid,
       person.displayname,
       maxuser.status,
       sod_Report.severity,
       sod_Report.scenario,
       RTRIM(XMLAGG(Xmlelement(E,groupuser.groupname,',').EXTRACT('//text()')   ORDER BY groupuser.groupname).GetClobVal(),',') groupname 
FROM   maxuser
       INNER JOIN person
               ON maxuser.userid = person.personid
       LEFT OUTER JOIN maximo.groupuser
                    ON maxuser.userid = groupuser.userid
                       AND maxuser.userid = person.personid
       LEFT OUTER JOIN sod_report
                    ON sod_report.userid = maxuser.userid
                       AND sod_report.userid = person.personid
WHERE  1 = 1
       AND (( maxuser.userid LIKE '%XX%' ))
GROUP  BY maxuser.userid,
          person.displayname,
          maxuser.status,
          sod_Report.severity,
          sod_Report.scenario 

输出如下

USERID  DISPLAYNAME  GROUPNAME 
 XXXX    Test User   A1,A1,B1,B1,C1,C1

预期输出

USERID  DISPLAYNAME  GROUPNAME 
XXXX    Test User    A1,B1,C1

我必须使用RTRIMXMLAGG,因为GROUPNAME的输出应以逗号分隔的单行显示。这是针对Oracle数据库的,请指教。

2 个答案:

答案 0 :(得分:0)

演示

在聚合之前添加一个使用ROW_NUMBER的附加步骤。

create table groupuser (groupname varchar2(100));

insert into groupuser (groupname) values ('A1');
insert into groupuser (groupname) values ('A1');
insert into groupuser (groupname) values ('B1');
insert into groupuser (groupname) values ('B1');
insert into groupuser (groupname) values ('C1');
insert into groupuser (groupname) values ('C1');
select  listagg (case when rn=1 then groupname end,',') within group (order by groupname)

from   (select  groupname
               ,row_number () over (partition by groupname order by null) as rn

        from    groupuser
        )
;
select  rtrim(xmlagg(case when rn=1 then xmlelement(e,groupname,',').extract('//text()') end order by groupname).getclobval(),',')  

    from   (select  groupname
                   ,row_number () over (partition by groupname order by null) as rn

            from    groupuser
            )
    ;
SELECT userid,
       displayname,
       status,
       severity,
       scenario,
       RTRIM(XMLAGG(Xmlelement(E,groupname,',').EXTRACT('//text()')   ORDER BY groupname).GetClobVal(),',') groupname 


FROM (
        SELECT maxuser.userid,
               person.displayname,
               maxuser.status,
               sod_Report.severity,
               sod_Report.scenario,
               groupuser.groupname,
               row_number () over (partition by groupuser.groupname order by null) as rn

        FROM   maxuser
               INNER JOIN person
                       ON maxuser.userid = person.personid
               LEFT OUTER JOIN maximo.groupuser
                            ON maxuser.userid = groupuser.userid
                               AND maxuser.userid = person.personid
               LEFT OUTER JOIN sod_report
                            ON sod_report.userid = maxuser.userid
                               AND sod_report.userid = person.personid
        WHERE  1 = 1
               AND (( maxuser.userid LIKE '%XX%' ))
    ) t



GROUP BY userid,
       displayname,
       status,
       severity,
       scenario
;              

答案 1 :(得分:0)

您可以使用正则表达式

select rtrim(regexp_replace('A1,A1,B1,B1,C1,C1','(\w+)(,(\1))+,?','\1,'),',') from dual