我想使用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()函数?
答案 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;