我使用LISTAGG
列了很多列。
所以我必须GROUP BY
所有这些列。
有更简洁的方法吗?
喜欢'按所有减去listagg分组...' ?
答案 0 :(得分:0)
你有一个示例表吗?你在加桌子吗?此示例使用连接,但LISTAGG列不在group by语句中。
以下是我编写的另一个问题的例子。
SELECT c.id,
c.model,
LISTAGG(d.name, ',')
within GROUP (ORDER BY d.name) AS "Drivers",
LISTAGG(c.color, ',')
within GROUP (ORDER BY c.color) AS "Colors"
FROM TBLCARS c
join TBLDRIVERS d
ON c.id = d.fid_tblcars
GROUP BY c.id,
c.model
ORDER BY c.id
这里使用WITH子句也可以使用CTE并构建更大的查询。
WITH cte
AS (SELECT c.id,
LISTAGG(c.color, ',')
within GROUP (ORDER BY c.color) AS "Colors",
LISTAGG(c.model, ',')
within GROUP (ORDER BY c.model) AS "Model",
LISTAGG(d.name, ',')
within GROUP (ORDER BY d.name) AS "Name"
FROM TBLCARS c
join TBLDRIVERS d
ON c.id = d.fid_tblcars
GROUP BY c.id)
SELECT *
FROM cte