使用带有连接表的LISTAGG函数

时间:2018-02-20 12:11:55

标签: sql oracle join listagg

我想使用LISTAGG()函数在我的SQL查询中聚合NAME列。我必须加入3桌。表PRI2PR是我的联接表。

表PROJ_INW

        ID
----------
        12 

表PROJEKT

ID     NAME
---  ---------------
61    Józef Dawidowski

表PRI2PR

ID    ID_PRIN    ID_PROJ 
---------- ---------- ---
9         12         61 

我的查询:

SELECT pi.ID,
LISTAGG(pr.NAME, ',') WITHIN GROUP (ORDER BY pr.NAME) AS PRO
FROM PROJ_INW pi
INNER JOIN PRI2PR pp ON pi.ID = pp.ID_PRIN
INNER JOIN PROJEKT pr ON pp.ID_PROJ = pr.ID
GROUP BY pi.ID, pr.NAME

问题:在我的案例中如何更正我的SQL语句以使用LISTAGG()函数?

1 个答案:

答案 0 :(得分:3)

您的查询基本上没问题。从pr.name

中删除group by
SELECT pi.ID,
       LISTAGG(pr.NAME, ',') WITHIN GROUP (ORDER BY pr.NAME) AS PRO
FROM PROJ_INW pi INNER JOIN
     PRI2PR pp
     ON pi.ID = pp.ID_PRIN INNER JOIN
     PROJEKT pr
     ON pp.ID_PROJ = pr.ID
GROUP BY pi.ID;