I am kind of newbie in Oracle. Got stuck in the below: I have the below 2 tables:
Site:
**SiteID|SiteName**
1 Sydney
2 Ne
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?
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;
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;
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;
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.