LISTAGG alternative in Oracle 10g

后端 未结 4 1909
轻奢々
轻奢々 2020-12-19 12:57

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         


        
4条回答
  •  一整个雨季
    2020-12-19 13:32

    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;
    

提交回复
热议问题