在一行中用逗号分隔结果

时间:2013-05-07 11:22:29

标签: sql oracle concatenation distinct

这是我写的查询:

select d.canonical_name,
       l.locale_id,
       d.domain_id,
       ld.display_name,
       p.canonical_name as Identity_Set
  from locale_domain ld
  join domain d on ld.domain_id = d.domain_id
  join locale l on l.locale_id = ld.locale_id
  join domain_set ds on ds.domain_id = d.domain_id
  join set_property sp on sp.domain_set_id = ds.domain_set_id
  join property p on p.property_id = sp.property_id
 where d.canonical_name = 'Video Games'
   and ds.canonical_name = 'IDENTITY_SET'
   group by l.locale_id,d.canonical_name,
       d.domain_id,
       ld.display_name,p.canonical_name
 order by l.locale_id

这是输出:

Video Games 0   1794    Games                   Video Game Name
Video Games 0   1794    Games                   Video Game Platform
Video Games 1   1794    Video Games_de_DE   Video Game Name
Video Games 1   1794    Video Games_de_DE   Video Game Platform
Video Games 2   1794    Video Games_en_GB   Video Game Name
Video Games 2   1794    Video Games_en_GB   Video Game Platform

这就是我需要的:

Video Games 0   1794    Games                  Video Game Name, Video Game Platform
Video Games 1   1794    Video Games_de_DE      Video Game Name, Video Game Platform
Video Games 2   1794    Video Games_en_GB      Video Game Name, Video Game Platform

你可以看到我需要它与众不同,我需要用逗号分隔的身份集的结果

谢谢

1 个答案:

答案 0 :(得分:0)

您可以使用Oracle 11中的listagg()函数执行此操作:

with t as (<your query here>)
select cannonical_name, domain_id, ld.display_name,
       listagg(identity_set, ',') within group (order by locale_id)
from t
group by cannonical_name, domain_id, ld.display_name