2016-12-26 181 views
0

我使用listagg()聚集字符串值甲骨文LISTAGG結果太長

舉例來說,有這樣的一個表:

Table1 
Name  Rank 
Bob  A 
Bob  B 
Bob  C 
Tom  A 
Tom  C 
SELECT Name,LISTAGG(RANK,';') WITHIN GROUP (ORDER BY RANK) AS COMRANK 
FROM Table1 

,我們得到的結果:

Name COMRANK 
Bob ABC 
Tom AC 

如果COMRANK變得太長會怎麼樣?在我彙總它們之前,我能找到最佳的xxx嗎?

回答

2

您可以使用窗口函數對行進行排名,並限制您需要的排名。

SELECT Name,LISTAGG(RANK,';') WITHIN GROUP (ORDER BY RANK) AS COMRANK 
FROM (select 
    name, rank, 
    rank() over (partition by name order by rank) rnk 
    from Table1 
) where rnk < 10; --some value 
GROUP BY Name; 
+0

它確實工作。謝謝 – Garfield