0

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 变得太长怎么办?我可以在汇总之前找到 top xxx 吗?

4

1 回答 1

3

您可以使用窗口函数对行进行排名,并将行限制为您需要的任何排名。

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;
于 2016-12-26T09:22:07.090 回答