LISTAGG alternative in Oracle 10g

假装没事ソ 提交于 2019-12-01 06:26:20

Try using XMLAGG like this:

select
    p.PeopleID,
    rtrim(xmlagg(xmlelement(e, s.SiteName, ',')).extract('//text()').getclobval(), ',')
from people p
join site s on p.SiteID = s.SiteID
group by p.PeopleID;

If you need the concatenation in a particular order, say increasing order of SiteId, then add an order by clause in the xmlagg:

select
    p.PeopleID,
    rtrim(xmlagg(xmlelement(e, s.SiteName, ',')
                   order by s.SiteId).extract('//text()').getclobval(), ',')
from people p
join site s on p.SiteID = s.SiteID
group by p.PeopleID;

EDIT:

If you want display result for all those people which are assigned to site 100:

select p.PeopleID,
    rtrim(xmlagg(
                xmlelement(e, s.SiteName, ',') order by s.SiteId
            ).extract('//text()').getclobval(), ',')
from people p
join site s on p.SiteID = s.SiteID
join (
    select distinct PeopleID
    from people
    where siteID = 1
    ) p2 on p.PeopleID = p2.PeopleID
group by p.PeopleID;

This is too long for a comment.

listagg() is the obvious choice, but it is not available in Oracle 10. However, even in Oracle 11, listagg() is limited to strings of length 4,000, and you explicitly say "Person can be associated with 1000+ locations".

There are ways around this, using CLOBs, XML, and no doubt other solutions as well. However, what use is a list of locations thousands and thousands of characters long? With so many locations, you are not going to be able to put the result in a standard varchar2() field.

Perhaps summarizing them in the database this way is not the best solution to your actual problem.

I think I am close to it, just need a small help: I have created a function GetSiteName, which returns the site name against the SiteID. Now I am using the below xmlagg where in I need to call this function GetSiteName:

select PeopleID, rtrim (xmlagg (xmlelement (e,  clint.GetSiteName(SiteID)   || ',')).extract ('//text()'), ',') SITEIDS
from client.People group by    PeopleID;/

Basically need help in calling the function from inside xmlagg, any thoughts?

I just needed an alternative for listagg on oracle 10g and found one in this page https://oracle-base.com/articles/misc/string-aggregation-techniques.

just use wm_concat, albeit it is not supported by oracle and has been droped in 12c.

with the example above:

select
    p.PeopleID,wm_concat(s.SiteName)
from people p
join site s on p.SiteID = s.SiteID
group by p.PeopleID;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!