简体   繁体   English

Oracle 10g中的LISTAGG替代方案

[英]LISTAGG alternative in Oracle 10g

I am kind of newbie in Oracle. 我是Oracle的新手。 Got stuck in the below: I have the below 2 tables: 卡在下面:我有以下2个表:

Site: 现场:

**SiteID|SiteName** 
1      Sydney
2      Newyork
3      Delhi

People: 人:

**RecordID|PeopleID|SiteID**
1         1        1
2         1        2
3         2        2
4         3        1
5         3        2
6         3        3

Now in my query I want an output something like this: 现在在我的查询中我想要一个像这样的输出:

**PeopleID | AssignedSites**
1          Sydney,NewYork
2          Newyork
3          Sydney,NewYork,Delhi
  • Few more points: 几点:

-The solution should work in Oracle 10g as well as 11g also. - 解决方案应该在Oracle 10g以及11g中运行。

-I have given small subset of data in the above example for brevity.But, in my prod scenario, one Person can be associated with 1000+ locations and there could 1000+ such person, so the solution should not break in that case! - 为了简洁起见,我在上面的示例中给出了一小部分数据。但是,在我的prod场景中,一个人可以与1000多个位置相关联,并且可能有1000多个这样的人,因此在这种情况下解决方案不应该破解!

Any help will be highly appreciated. 任何帮助将受到高度赞赏。

Thanks in advance. 提前致谢。

Try using XMLAGG like this: 尝试使用XMLAGG如下所示:

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: 如果您需要以特定顺序连接,例如增加SiteId的order by ,则在xmlagg中添加order by子句:

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: 如果要显示分配给站点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". listagg()是显而易见的选择,但在Oracle 10中不可用。但是,即使在Oracle 11中, listagg()仅限于长度为4,000的字符串,并且您明确说“Person可以与1000多个位置相关联”。

There are ways around this, using CLOBs, XML, and no doubt other solutions as well. 有很多方法可以解决这个问题,使用CLOB,XML,以及毫无疑问的其他解决方案。 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. 如此多的位置,您将无法将结果放在标准的varchar2()字段中。

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. 我想我很接近它,只需要一个小帮助:我创建了一个函数Ge​​tSiteName,它返回SiteID的站点名称。 Now I am using the below xmlagg where in I need to call this function GetSiteName: 现在我使用下面的xmlagg,我需要调用此函数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? 基本上需要帮助从xmlagg内部调用函数,有什么想法吗?

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 . 我只需要在oracle 10g上使用listagg的替代方案,并在此页面中找到一个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. 只是使用wm_concat,虽然它不受oracle的支持,并已在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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM