Oracle 中 select 语句结果的逗号分隔列表

发布于 2024-10-22 12:52:03 字数 249 浏览 5 评论 0原文

我有一个名为“人”的表。它包含人员的 ID 及其父 ID(只能有一个父 ID)。作为查询的结果,我想要一个具有第一列(人员 ID)和第二列(其子 ID 列表)的表。具体如何做到这一点?我已经阅读过有关 listagg 函数的信息,但我不确定它是否适合我的目的。 此查询生成一个空的第二列:

select t1.id, (从人员 t2 中选择 t2.id 其中 t2.parent_id = t1.id) 来自人 t1 其中 t1.status = '父级';

I have a table named "person". It contains person's id and it's parent id (only one parent is possible). As a result of a query, I want a table with first column - a person id, and a second column - a list of it's children id's. How exactly to do this? I've read about listagg function, but I'm not sure if it is appropriate for my purpose.
And this query produces an empty second column:

select t1.id,
(select t2.id from person t2
where t2.parent_id = t1.id) from person t1
where t1.status = 'parent';

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

哆啦不做梦 2024-10-29 12:52:03
SELECT parent_id,
       RTRIM(XMLAGG(XMLELEMENT(e,child_id || ',')).EXTRACT('//text()'),',') AS "Children"
  FROM parentChildTable
 WHERE parent_id = 0
 GROUP BY parent_id

或者

SELECT parent_id,
       LISTAGG(child_id, ',') WITHIN GROUP (ORDER BY child_id) AS "Children"
  FROM parentChildTable
 WHERE parent_id = 0
 GROUP BY parent_id
SELECT parent_id,
       RTRIM(XMLAGG(XMLELEMENT(e,child_id || ',')).EXTRACT('//text()'),',') AS "Children"
  FROM parentChildTable
 WHERE parent_id = 0
 GROUP BY parent_id

or

SELECT parent_id,
       LISTAGG(child_id, ',') WITHIN GROUP (ORDER BY child_id) AS "Children"
  FROM parentChildTable
 WHERE parent_id = 0
 GROUP BY parent_id
眼角的笑意。 2024-10-29 12:52:03

Mark 的 LISTAGG 实现绝对是 ORACLE 11GR2 的最佳选择。对于 11GR1 或 Oracle 10,您可以以完全相同的方式使用 wmsys.wm_Concat (可能需要 DBA 授予权限)

Mark's implementation of LISTAGG is definitely the way to go for ORacle 11GR2. For For 11GR1 or Oracle 10 you can use wmsys.wm_Concat instead in exactly the same way (may require a permissions grant from your DBA)

窝囊感情。 2024-10-29 12:52:03

只是另一种方法来接近它......

SELECT parent_id,max(child_list) FROM (
  SELECT parent_id,sys_connect_by_path(child_number,',') child_list FROM (
    SELECT parent_id, id,
           row_number() over (partition by parent_id order by id) child_number
      FROM person
      WHERE parent_id IS NOT NULL
  )
  START WITH child_number=1
  CONNECT BY parent_id = PRIOR parent_id AND child_number = PRIOR child_number + 1
)
GROUP BY parent_id
ORDER BY parent_id
;

Just another way to approach it ...

SELECT parent_id,max(child_list) FROM (
  SELECT parent_id,sys_connect_by_path(child_number,',') child_list FROM (
    SELECT parent_id, id,
           row_number() over (partition by parent_id order by id) child_number
      FROM person
      WHERE parent_id IS NOT NULL
  )
  START WITH child_number=1
  CONNECT BY parent_id = PRIOR parent_id AND child_number = PRIOR child_number + 1
)
GROUP BY parent_id
ORDER BY parent_id
;
凤舞天涯 2024-10-29 12:52:03

SELECT wmsys.wm_concat()
从 ;

这是有争议的,但它有效 - https://forums.oracle.com/forums/ thread.jspa?threadID=2205545

SELECT wmsys.wm_concat()
FROM ;

It's controversial, but it works - https://forums.oracle.com/forums/thread.jspa?threadID=2205545

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文